How to change to a different frame from sprite sheet?

Hey guys, 

I can’t find how to swap an image to a different one from the sprite sheet.

I have an object “gem” with ‘gem.MyGemID’ attribute

And I set up a sprite sheet like this:

[lua]
local gemSheetOptions = {

    width = 80,

    height = 80,

    numFrames = 6,

}

local gemSheet = graphics.newImageSheet ( “images/gemSheet.png” ,gemSheetOptions )

[/lua]

Now I have the gem setup like this:

[lua]
local gem = display.newImage( gemSheet, 1 ) – starting image is 1

gem.width = 40

gem.height = 40

gem.x = 20

gem.y = 424

gem.MyRow = (gem.y - firstRowY + gemDimentions)/gemDimentions

gem.MyCol = (gem.x - firstColX + gemDimentions)/gemDimentions

gem.MyGemID = board[gem.MyRow][gem.MyCol]

– Now i would like to change its image based on its gem.MyGemID from the sheet

[/lua]

And I want to swap its image from the sprite sheet according to its “gem.MyGemID” variable…

And I cant seem to find how to do this.

Im a total newbie so go easy on me :slight_smile:

Any help will be greatly appreciated

Roy.

Hey Roy!

Sprites use the method setFrame() to go to a specific image in the spritesheet.

In your case it would be:

gem:setFrame(MyGemID)

Joe!!

Nice to see you here :slight_smile:

Hope mozaika is coming along well

I actually tried what you suggested, it doesnt work for some reason :confused:

I struggle all day to do something so simple, 

I have a sprite sheet with frames, some frames are statics, some frames i need to animate.

Meaning, depending on the value of gem.MyGemID, this object can either use a single frame from the sheet, or animate few frames…

here is my code:

[lua]

local gemSheetOptions = {

    width = 80,

    height = 80,

    numFrames = 18,

    sheetContentWidth = 480, 

    sheetContentHeight = 320,

}

local gemSheet = graphics.newImageSheet ( “images/gemSheet.png” ,gemSheetOptions )

local sequenceData = {

– gem 1 - single frame

    {    name = “gem1” ,frames = {1} ,time = 1000 },

– gem 2 - single frame

    {    name = “gem2” ,frames = {2} ,time = 1000 },

– gem 3 - single frame

    {    name = “gem3” ,frames = {3} ,time = 1000 },

– gem 4 - single frame

    {    name = “gem4” ,frames = {4} ,time = 1000 },

– gem 5 - single frame

    {    name = “gem5” ,frames = {5} ,time = 1000},

– gem 6 - single frame

    {    name = “gem6” ,frames = {6} ,time = 1000 },

– gem 1 - animate power up 1

    {    name = “gem101” ,frames = {7,8,9,10,11} ,time = 1000 },

– gem 1 - animate power up 2

    {    name = “gem201” ,frames = {12,13,14,15,16} ,time = 1000 },

}

local gem = display.newSprite( gemSheet, sequenceData )

gem.width = 40

gem.height = 40

gem.x = 20

gem.y = 424

gem.MyRow = (gem.y - firstRowY + gemDimentions)/gemDimentions – calculate row

gem.MyCol = (gem.x - firstColX + gemDimentions)/gemDimentions – calculate col

gem.MyGemID = board[gem.MyRow][gem.MyCol] – getting self ID from table

gem:setSequence( “gem” … gem.MyGemID ) 

gem:play() 

gem.xScale=0.5

gem.yScale=0.5

[/lua]

Am I doing this the wrong way?

Roy.

Hi @roysadaka,

Setting the sequence is the correct method, followed by setting the frame within that sequence. One common point of confusion is that when you set the frame within the sequence, it’s relative to the index of that sequence, not the overall sheet. So, if you have a sequence that is {7,8,9,10,11}, and you want to set the frame to “frame #9” of the overall sheet, you should set the frame to 3 (not 9) since 9 is in the 3rd index spot of that sequence. Corona will throw a warning (but not a crash) if you attempt to set the frame beyond the total number of indices in the sequence, and then it will revert to the first frame… so keep an eye on the Terminal/console and make sure you’re not getting that warning.

If you’re still getting unexpected behavior after confirming this, I suspect there’s just some issue with the index it’s trying to pick up from elsewhere in your code.

Hope this helps,

Brent Sorrentino

Got this one to work thank you!

One more question about this, like iv’e mentioned, some sequences are only a single frame, for example the first one in the sequenceData , is this fine?  

Roy.

Hi Roy,

This is basically fine, but for the single-frame “sequences”, you could save yourself some coding by making one sequence that contains every frame, then just set that sequence along with the frame you need. Same basic thing will happen in the end, but it’s just less code. :slight_smile:

Take care,

Brent

Hey Roy!

Sprites use the method setFrame() to go to a specific image in the spritesheet.

In your case it would be:

gem:setFrame(MyGemID)

Joe!!

Nice to see you here :slight_smile:

Hope mozaika is coming along well

I actually tried what you suggested, it doesnt work for some reason :confused:

I struggle all day to do something so simple, 

I have a sprite sheet with frames, some frames are statics, some frames i need to animate.

Meaning, depending on the value of gem.MyGemID, this object can either use a single frame from the sheet, or animate few frames…

here is my code:

[lua]

local gemSheetOptions = {

    width = 80,

    height = 80,

    numFrames = 18,

    sheetContentWidth = 480, 

    sheetContentHeight = 320,

}

local gemSheet = graphics.newImageSheet ( “images/gemSheet.png” ,gemSheetOptions )

local sequenceData = {

– gem 1 - single frame

    {    name = “gem1” ,frames = {1} ,time = 1000 },

– gem 2 - single frame

    {    name = “gem2” ,frames = {2} ,time = 1000 },

– gem 3 - single frame

    {    name = “gem3” ,frames = {3} ,time = 1000 },

– gem 4 - single frame

    {    name = “gem4” ,frames = {4} ,time = 1000 },

– gem 5 - single frame

    {    name = “gem5” ,frames = {5} ,time = 1000},

– gem 6 - single frame

    {    name = “gem6” ,frames = {6} ,time = 1000 },

– gem 1 - animate power up 1

    {    name = “gem101” ,frames = {7,8,9,10,11} ,time = 1000 },

– gem 1 - animate power up 2

    {    name = “gem201” ,frames = {12,13,14,15,16} ,time = 1000 },

}

local gem = display.newSprite( gemSheet, sequenceData )

gem.width = 40

gem.height = 40

gem.x = 20

gem.y = 424

gem.MyRow = (gem.y - firstRowY + gemDimentions)/gemDimentions – calculate row

gem.MyCol = (gem.x - firstColX + gemDimentions)/gemDimentions – calculate col

gem.MyGemID = board[gem.MyRow][gem.MyCol] – getting self ID from table

gem:setSequence( “gem” … gem.MyGemID ) 

gem:play() 

gem.xScale=0.5

gem.yScale=0.5

[/lua]

Am I doing this the wrong way?

Roy.

Hi @roysadaka,

Setting the sequence is the correct method, followed by setting the frame within that sequence. One common point of confusion is that when you set the frame within the sequence, it’s relative to the index of that sequence, not the overall sheet. So, if you have a sequence that is {7,8,9,10,11}, and you want to set the frame to “frame #9” of the overall sheet, you should set the frame to 3 (not 9) since 9 is in the 3rd index spot of that sequence. Corona will throw a warning (but not a crash) if you attempt to set the frame beyond the total number of indices in the sequence, and then it will revert to the first frame… so keep an eye on the Terminal/console and make sure you’re not getting that warning.

If you’re still getting unexpected behavior after confirming this, I suspect there’s just some issue with the index it’s trying to pick up from elsewhere in your code.

Hope this helps,

Brent Sorrentino

Got this one to work thank you!

One more question about this, like iv’e mentioned, some sequences are only a single frame, for example the first one in the sequenceData , is this fine?  

Roy.

Hi Roy,

This is basically fine, but for the single-frame “sequences”, you could save yourself some coding by making one sequence that contains every frame, then just set that sequence along with the frame you need. Same basic thing will happen in the end, but it’s just less code. :slight_smile:

Take care,

Brent