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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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)

From reading the API on sprite:prepare, I'm not sure it will reset to the first frame if you don't have a sequence parameter.
"... optionally sets the new current sequence, and moves to the first frame of that sequence. "

What happens if you add the sequence name? Does it reset to the first frame?

I'm add the sequence name.
See line 15 in original post.

1
timer.performWithDelay(3000, function() instance:prepare("run") end)

Sorry, I didn't see that you had the sequence name. The issue is the prepare method does reset the frame index but doesn't render the frame. When you run play, it doesn't render the frame until it transitions to the second frame. This results in skipping the first frame when you restart the sprite.

We think the solution is to have prepare render the new frame so which will show the reset frame index when you call prepare and play will look like it's working when called after prepare.

views:1276 update:2011/10/10 9:00:20
corona forums © 2003-2011