Trouble starting sprite animation on specific frame

Hello,

I’m trying to start playback of a sprite on a specific frame index, rather than starting on frame 1. However, when I do this, Corona displays frame 1, then skips to the frame after the frame index I set as the starting point.

This is my sequenceData:

[lua]{
name=“animSequence”,
frames={1,2,3,4,5},
loopCount = 1
}[/lua]

And this is the playback code:

[lua]mySprite:setSequence(“animSequence”)
mySprite:setFrame(2)
mySprite:play()[/lua]

In this example, frame 1 would display, followed by frames 3, 4, and 5. However, Corona reports (via mySprite.frame) that frames 2, 3, 4, and 5 are played, despite what I’m seeing visually in the simulator.

Anything I’m doing wrong or misunderstanding?

Thanks!

  • David [import]uid: 149111 topic_id: 34012 reply_id: 334012[/import]

Your snips seem like they should work as expected.

Are you setting a time in sequenceData ?

If not, try that and make the interval long for debugging.
And try just setFrame to different frames without play.

Long shot:
Are you using uniform cells in your animation image ?
iow: you are not having to specify x, y, width and height for each frame.

Irregular frames have caused me some problems during play, but inconsistently.
For example - the animation stops on the first frame but only occasionally.
[import]uid: 186251 topic_id: 34012 reply_id: 135271[/import]

@same3dus: Thanks for the help! I’m not setting a time in sequenceData, though I’ve tried setting a long interval for debugging, like you suggested. That’s how I am so sure that the wrong frame is being displayed every time. My frames are not uniform, though I haven’t experienced any trouble with non-uniform frame sizes so far. Still, that could very well be it.

At this point I’ve worked around it by creating additional sequenceData animation sequences. Rather than setting the start frame, I set the sequence that starts on the frame I want. This works, but it clutters up my Lua file and is cumbersome.

Anyone else have this problem? It seems like a bug, as printing object.frame in the above example reports that frame 2 is being displayed, even though frame 1 is clearly on screen. [import]uid: 149111 topic_id: 34012 reply_id: 135299[/import]

Hi David,
Can you please post your image sheet setup as well, so I can see how it relates to your sprites?

Thanks,
Brent [import]uid: 200026 topic_id: 34012 reply_id: 135301[/import]

A bit more info for Brent -

setFrame does not behave as expected… ?
Even with uniform frames…

( maybe we should be using sprite.newSpriteSheet instead of graphics.newImageSheet ? )

 local catAnimOptions =  
 {  
 width = 50,  
 height = 50,  
 numFrames = 4,  
  
 sheetContentWidth = 200,   
 sheetContentHeight = 50   
 }  
 local catImageSheet = graphics.newImageSheet( "images/cat.png", catAnimOptions )  
  
 local catSequenceData = {  
 { name="smile", start=4, count=1, time=10, loopCount = 1 },  
 { name="ouch", frames={ 2, 1 }, time=250, loopCount = 1 },   
 { name="snarl", frames={ 3, 1 }, time=250, loopCount = 1 },  
 }  
  
 cat:setSequence('smile')  
 cat:play()  

The above works fine, but since ‘smile’ is a single frame, why not just go to that frame ?

Because commenting out setSeqence and :play and using this:

cat:SetFrame(4)

results in this:
ERROR: sprite:setFrame() given invalid index (4). Using index of 2 instead.

And the sprite shows Frame 1, not Frame 2.

also - it seems that the first sequence in SequenceData is always played as soon as the sprite is added to the stage, even if you don’t explicitedly use set/Sequence and play

[import]uid: 186251 topic_id: 34012 reply_id: 135348[/import]

Hi @sam3dus,

What’s happening here is basically this: if you declare a sprite, you need to also declare a sequence (obvious enough, you’re doing that). Once you declare that sequence, it becomes “independent” of the overall image sheet frame count, at least in terms of the count. So, when you set up a sequence of 2 frames, it has 2 total frames even if those image sheet frames are #3-4 or whatever.

So, if you want a “smile” sequence, all you need to do is declare its sequence as start=4, count=1, and then “setFrame(1)” instead of 4. Also you don’t need to play it, obviously, since it doesn’t actually animate.

Hope this helps!
Brent [import]uid: 200026 topic_id: 34012 reply_id: 135367[/import]

@Brent Sorrentino: No problem, thanks for your help!

[lua]-- Load image sheet info and create image sheet
local imageSheetInfo = require(“sprites.Gum_Boot_Anim”)
local imageSheet = graphics.newImageSheet(“sprites/Gum_Boot_Anim.png”, imageSheetInfo:getSheet())[/lua]

The image sheet was created with TexturePacker. Here’s a snippet:

[lua]local SheetInfo = {}

SheetInfo.sheet =
{
frames = {

{
– GumBoot01@2x
x=74,
y=864,
width=70,
height=110,

sourceX = 32,
sourceY = 222,
sourceWidth = 193,
sourceHeight = 337
},
{
– GumBoot02@2x
x=2,
y=864,
width=70,
height=130,

sourceX = 32,
sourceY = 202,
sourceWidth = 193,
sourceHeight = 337
},
{
– GumBoot03@2x
x=146,
y=852,
width=70,
height=150,

sourceX = 32,
sourceY = 182,
sourceWidth = 193,
sourceHeight = 337
},

– etc.
SheetInfo.frameIndex =
{

[“GumBoot01@2x”] = 1,
[“GumBoot02@2x”] = 2,
[“GumBoot03@2x”] = 3,
[“GumBoot04@2x”] = 4,
[“GumBoot05@2x”] = 5,
[“GumBoot06@2x”] = 6,
[“GumBoot07@2x”] = 7,
[“GumBoot08@2x”] = 8,
[“GumBoot09@2x”] = 9,
[“GumBoot10@2x”] = 10,
[“GumBoot11@2x”] = 11,
[“GumBoot13@2x”] = 12,
[“GumBoot15@2x”] = 13,
[“GumBoot17@2x”] = 14,
[“GumBoot19@2x”] = 15,
[“GumBoot21@2x”] = 16,
[“GumBoot23@2x”] = 17,
[“GumBoot25@2x”] = 18,
[“GumBoot27@2x”] = 19,
[“GumBoot29@2x”] = 20,
[“GumBoot31@2x”] = 21,
[“GumBoot35@2x”] = 22,
[“GumBoot33@2x”] = 23,
}

function SheetInfo:getSheet()
return self.sheet;
end

function SheetInfo:getFrameIndex(name)
return self.frameIndex[name];
end

return SheetInfo[/lua] [import]uid: 149111 topic_id: 34012 reply_id: 135370[/import]

Your snips seem like they should work as expected.

Are you setting a time in sequenceData ?

If not, try that and make the interval long for debugging.
And try just setFrame to different frames without play.

Long shot:
Are you using uniform cells in your animation image ?
iow: you are not having to specify x, y, width and height for each frame.

Irregular frames have caused me some problems during play, but inconsistently.
For example - the animation stops on the first frame but only occasionally.
[import]uid: 186251 topic_id: 34012 reply_id: 135271[/import]

@same3dus: Thanks for the help! I’m not setting a time in sequenceData, though I’ve tried setting a long interval for debugging, like you suggested. That’s how I am so sure that the wrong frame is being displayed every time. My frames are not uniform, though I haven’t experienced any trouble with non-uniform frame sizes so far. Still, that could very well be it.

At this point I’ve worked around it by creating additional sequenceData animation sequences. Rather than setting the start frame, I set the sequence that starts on the frame I want. This works, but it clutters up my Lua file and is cumbersome.

Anyone else have this problem? It seems like a bug, as printing object.frame in the above example reports that frame 2 is being displayed, even though frame 1 is clearly on screen. [import]uid: 149111 topic_id: 34012 reply_id: 135299[/import]

Hi David,
Can you please post your image sheet setup as well, so I can see how it relates to your sprites?

Thanks,
Brent [import]uid: 200026 topic_id: 34012 reply_id: 135301[/import]

A bit more info for Brent -

setFrame does not behave as expected… ?
Even with uniform frames…

( maybe we should be using sprite.newSpriteSheet instead of graphics.newImageSheet ? )

 local catAnimOptions =  
 {  
 width = 50,  
 height = 50,  
 numFrames = 4,  
  
 sheetContentWidth = 200,   
 sheetContentHeight = 50   
 }  
 local catImageSheet = graphics.newImageSheet( "images/cat.png", catAnimOptions )  
  
 local catSequenceData = {  
 { name="smile", start=4, count=1, time=10, loopCount = 1 },  
 { name="ouch", frames={ 2, 1 }, time=250, loopCount = 1 },   
 { name="snarl", frames={ 3, 1 }, time=250, loopCount = 1 },  
 }  
  
 cat:setSequence('smile')  
 cat:play()  

The above works fine, but since ‘smile’ is a single frame, why not just go to that frame ?

Because commenting out setSeqence and :play and using this:

cat:SetFrame(4)

results in this:
ERROR: sprite:setFrame() given invalid index (4). Using index of 2 instead.

And the sprite shows Frame 1, not Frame 2.

also - it seems that the first sequence in SequenceData is always played as soon as the sprite is added to the stage, even if you don’t explicitedly use set/Sequence and play

[import]uid: 186251 topic_id: 34012 reply_id: 135348[/import]

Hi @sam3dus,

What’s happening here is basically this: if you declare a sprite, you need to also declare a sequence (obvious enough, you’re doing that). Once you declare that sequence, it becomes “independent” of the overall image sheet frame count, at least in terms of the count. So, when you set up a sequence of 2 frames, it has 2 total frames even if those image sheet frames are #3-4 or whatever.

So, if you want a “smile” sequence, all you need to do is declare its sequence as start=4, count=1, and then “setFrame(1)” instead of 4. Also you don’t need to play it, obviously, since it doesn’t actually animate.

Hope this helps!
Brent [import]uid: 200026 topic_id: 34012 reply_id: 135367[/import]

@Brent Sorrentino: No problem, thanks for your help!

[lua]-- Load image sheet info and create image sheet
local imageSheetInfo = require(“sprites.Gum_Boot_Anim”)
local imageSheet = graphics.newImageSheet(“sprites/Gum_Boot_Anim.png”, imageSheetInfo:getSheet())[/lua]

The image sheet was created with TexturePacker. Here’s a snippet:

[lua]local SheetInfo = {}

SheetInfo.sheet =
{
frames = {

{
– GumBoot01@2x
x=74,
y=864,
width=70,
height=110,

sourceX = 32,
sourceY = 222,
sourceWidth = 193,
sourceHeight = 337
},
{
– GumBoot02@2x
x=2,
y=864,
width=70,
height=130,

sourceX = 32,
sourceY = 202,
sourceWidth = 193,
sourceHeight = 337
},
{
– GumBoot03@2x
x=146,
y=852,
width=70,
height=150,

sourceX = 32,
sourceY = 182,
sourceWidth = 193,
sourceHeight = 337
},

– etc.
SheetInfo.frameIndex =
{

[“GumBoot01@2x”] = 1,
[“GumBoot02@2x”] = 2,
[“GumBoot03@2x”] = 3,
[“GumBoot04@2x”] = 4,
[“GumBoot05@2x”] = 5,
[“GumBoot06@2x”] = 6,
[“GumBoot07@2x”] = 7,
[“GumBoot08@2x”] = 8,
[“GumBoot09@2x”] = 9,
[“GumBoot10@2x”] = 10,
[“GumBoot11@2x”] = 11,
[“GumBoot13@2x”] = 12,
[“GumBoot15@2x”] = 13,
[“GumBoot17@2x”] = 14,
[“GumBoot19@2x”] = 15,
[“GumBoot21@2x”] = 16,
[“GumBoot23@2x”] = 17,
[“GumBoot25@2x”] = 18,
[“GumBoot27@2x”] = 19,
[“GumBoot29@2x”] = 20,
[“GumBoot31@2x”] = 21,
[“GumBoot35@2x”] = 22,
[“GumBoot33@2x”] = 23,
}

function SheetInfo:getSheet()
return self.sheet;
end

function SheetInfo:getFrameIndex(name)
return self.frameIndex[name];
end

return SheetInfo[/lua] [import]uid: 149111 topic_id: 34012 reply_id: 135370[/import]