how to loop a sprite with delay

Say, I have a Sprite having 5 frames. I want to play Frame 1 to 5, then wait for 1000 milliseconds, and then play frame 1 to 5 again?

How can i do this?

Currently i’m using loop index 0, so the frames go from 1 to 5, and then 1 to 5 again and so on. How can i put a delay between these iterations?

Thanks [import]uid: 175611 topic_id: 31962 reply_id: 331962[/import]

I see no one answered this yet, so I’ll give it a try.

Let’s say that your sprite Spr plays “sequence” (which is defined as frames 1 to 5, non-looping) in one second. So what you would do is something like this:

local function TwoSecondTimer()  
 Spr:prepare("sequence")  
 Spr:play()  
end  
  
TwoSecTimer=timer.performWithDelay(2000,TwoSecondTimer,0)  

Note that this is for old-style-sprites. But easily changed to new-style ones if you like. [import]uid: 160496 topic_id: 31962 reply_id: 127520[/import]

Mike’s solution is good, but it assumes that your sprite will always animate over 1 second (1 second plus 1 second delay, so 2 seconds for the timer repeat). An alternative method would be to use an onComplete listener to run a 1 second timer, and upon its completion you reset and restart the sprite.

Brent [import]uid: 9747 topic_id: 31962 reply_id: 127544[/import]

Mike’s Solution works perfectly for my case, thanks a lot.

I have another similar question now:

I have another sprite which goes from frame 1 to 5 in 500 milliseconds (means each frame is shown for 100 ms). I want the sprite to freeze at 5th frame for, say 300 milliseconds, and then move in reverse direction (from frame 5 to 1 in 500 milliseconds)

at the moment i’m using loop index of -1, which means my sprite goes from frame 1 to 5 and back to 1 in 1000 ms. I want to add a delay before the sprite goes in revers direction. [import]uid: 175611 topic_id: 31962 reply_id: 127555[/import]

Have a sprite sequence that goes 1 to 5 and back - looping. Use the “sprite” event listener on the sprite. Use the “next” phase in the listener and when the currentFrame reaches 5 do a pause() on the sprite, and at the same time fire a delayed timer for 300 milliseconds that would play() the sprite again. [import]uid: 160496 topic_id: 31962 reply_id: 127559[/import]

I understand, but i’m having problems with the syntax. could you give me a sample code, please? [import]uid: 175611 topic_id: 31962 reply_id: 127563[/import]

[code]
local function SpriteEvent(e)
if e.phase==“next” and e.sprite.currentFrame==5 then
e:pause();
local function restartSprite()
e.sprite:play()
end
timer.performWithDelay(300,restartSprite,1)
end
end

sprite.add( SpriteSet, “BackAndForth”, 1,5, 500, -2) – loops back and forth forever

mysprite=sprite.newSprite( SpriteSet )
mysprite:addEventListener(“sprite”, SpriteEvent)
mysprite:prepare(“BackAndForth”)
mysprite:play()
[/code] [import]uid: 160496 topic_id: 31962 reply_id: 127567[/import]

Got it. It works. Thanks a lot :slight_smile: [import]uid: 175611 topic_id: 31962 reply_id: 127570[/import]

I see no one answered this yet, so I’ll give it a try.

Let’s say that your sprite Spr plays “sequence” (which is defined as frames 1 to 5, non-looping) in one second. So what you would do is something like this:

local function TwoSecondTimer()  
 Spr:prepare("sequence")  
 Spr:play()  
end  
  
TwoSecTimer=timer.performWithDelay(2000,TwoSecondTimer,0)  

Note that this is for old-style-sprites. But easily changed to new-style ones if you like. [import]uid: 160496 topic_id: 31962 reply_id: 127520[/import]

Mike’s solution is good, but it assumes that your sprite will always animate over 1 second (1 second plus 1 second delay, so 2 seconds for the timer repeat). An alternative method would be to use an onComplete listener to run a 1 second timer, and upon its completion you reset and restart the sprite.

Brent [import]uid: 9747 topic_id: 31962 reply_id: 127544[/import]

Mike’s Solution works perfectly for my case, thanks a lot.

I have another similar question now:

I have another sprite which goes from frame 1 to 5 in 500 milliseconds (means each frame is shown for 100 ms). I want the sprite to freeze at 5th frame for, say 300 milliseconds, and then move in reverse direction (from frame 5 to 1 in 500 milliseconds)

at the moment i’m using loop index of -1, which means my sprite goes from frame 1 to 5 and back to 1 in 1000 ms. I want to add a delay before the sprite goes in revers direction. [import]uid: 175611 topic_id: 31962 reply_id: 127555[/import]

Have a sprite sequence that goes 1 to 5 and back - looping. Use the “sprite” event listener on the sprite. Use the “next” phase in the listener and when the currentFrame reaches 5 do a pause() on the sprite, and at the same time fire a delayed timer for 300 milliseconds that would play() the sprite again. [import]uid: 160496 topic_id: 31962 reply_id: 127559[/import]

I understand, but i’m having problems with the syntax. could you give me a sample code, please? [import]uid: 175611 topic_id: 31962 reply_id: 127563[/import]

[code]
local function SpriteEvent(e)
if e.phase==“next” and e.sprite.currentFrame==5 then
e:pause();
local function restartSprite()
e.sprite:play()
end
timer.performWithDelay(300,restartSprite,1)
end
end

sprite.add( SpriteSet, “BackAndForth”, 1,5, 500, -2) – loops back and forth forever

mysprite=sprite.newSprite( SpriteSet )
mysprite:addEventListener(“sprite”, SpriteEvent)
mysprite:prepare(“BackAndForth”)
mysprite:play()
[/code] [import]uid: 160496 topic_id: 31962 reply_id: 127567[/import]

Got it. It works. Thanks a lot :slight_smile: [import]uid: 175611 topic_id: 31962 reply_id: 127570[/import]