sprite:prepare() is expected to move to the first frame of the named sequence. This doesn't happen though.

In the sample code attached, i start sprite animation and stop it by calling sprite:prepare(“run”) after 3 sec timeout. When timer fires, animation stops, but the sprite is not reset to the first frame. I think this is a bug. See the code attached.

[code]
local sprite = require(“sprite”)
local spriteSheet = sprite.newSpriteSheet(“greenman.png”, 128, 128)
local spriteSet = sprite.newSpriteSet(spriteSheet, 1, 15)

sprite.add(spriteSet, “run”, 1, 15, 1000, 0)

local instance = sprite.newSprite(spriteSet)
instance.x = 50
instance.y = 100

local function run()
instance:prepare(“run”)
instance:play()
– stop some time later
timer.performWithDelay(3000, function() instance:prepare(“run”) end)
end

Runtime:addEventListener(“tap”, function ()
run()
return true
end)
[/code] [import]uid: 75889 topic_id: 13170 reply_id: 313170[/import]

so you want first frame ??

http://developer.anscamobile.com/reference/index/spriteinstancecurrentframe

[import]uid: 35857 topic_id: 13170 reply_id: 48306[/import]

spriteInstance:prepare()
“Stops any currently playing animation sequence on the sprite instance spriteInstance, optionally sets the new current sequence, and moves to the first frame of that sequence.”

Why it doesn’t work? Animation stops on random frame. [import]uid: 75889 topic_id: 13170 reply_id: 48310[/import]

Krill,

Calling spriteInstance:prepare() does reset the animation to the first frame, BUT it also stops the animation. That first frame won’t be drawn until you call the play() function again. So, your sprite is showing the last frame it was displayed since the prepare() function was called. This was the intended design.

I recommend that you work around this by setting up a second sprite set having only the 1st frame in it. You can then prepare() and play() this animation having only 1 frame. [import]uid: 32256 topic_id: 13170 reply_id: 48447[/import]

Is it possible to do by setting spriteInstance.currentFrame = 1 after spriteInstance:prepare() call? Or can it cause some problems further in code/“sprite behavior”?

like this:
[lua]timer.performWithDelay(3000, function() instance:prepare(“run”) instance.currentFrame = 1 end)[/lua] [import]uid: 78572 topic_id: 13170 reply_id: 48456[/import]

Seppanen,

No, that won’t work. A sprite will only draw a newly indexed frame if it is currently “playing”. Calling the prepare() function will stop the sprite animation, causing it to only draw the last “played” frame in the animation. What I’ve suggested above is the only work-around that I can think of. [import]uid: 32256 topic_id: 13170 reply_id: 48463[/import]