Playing sprite for fixed period of time

Hi,

I’m using a sprite sheet of a vehicle. I start the animation when the vehicle is touched. But I want the animation to stop after a fixed period of time, say 2000 milliseconds. Then I want to start the vehicle again upon touch, for 2000 ms.

How can I do that? [import]uid: 175611 topic_id: 31297 reply_id: 331297[/import]

Should be simple: on every start of the vehicle, just execute a delay timer for 2000 ms. When it finishes, just call a function to stop (pause, actually) the animation. Also make sure that the touch can’t trigger multiple timers at once, by checking if a delay timer for this action is already running.

Does that help?

Brent [import]uid: 9747 topic_id: 31297 reply_id: 125087[/import]

Yes, I understand what you’re saying. But i’m having difficulty implementing it. could you please help with the syntax a bit? [import]uid: 175611 topic_id: 31297 reply_id: 125090[/import]

Sure, it should look something like this:

local timerRunning = false --flag to monitor if timer is running or not running  
  
local function pauseSprite ()  
 --PAUSE your sprite animation here (whatever code you're using for that)  
 timerRunning = false --reset flag because timer has stopped (thus, is now false)  
end  
  
--this function is called when the sprite begins animation (after a touch)  
local function startTimer ()   
 if ( timerRunning == false ) then  
 timer.performWithDelay( 2000, pauseSprite )  
 timerRunning = true  
 end  
end  

Give that a test and let me know if it works.

Brent
[import]uid: 9747 topic_id: 31297 reply_id: 125143[/import]

It works. Issue Resolved. Thanks a lot :slight_smile: [import]uid: 175611 topic_id: 31297 reply_id: 125218[/import]

Should be simple: on every start of the vehicle, just execute a delay timer for 2000 ms. When it finishes, just call a function to stop (pause, actually) the animation. Also make sure that the touch can’t trigger multiple timers at once, by checking if a delay timer for this action is already running.

Does that help?

Brent [import]uid: 9747 topic_id: 31297 reply_id: 125087[/import]

Yes, I understand what you’re saying. But i’m having difficulty implementing it. could you please help with the syntax a bit? [import]uid: 175611 topic_id: 31297 reply_id: 125090[/import]

Sure, it should look something like this:

local timerRunning = false --flag to monitor if timer is running or not running  
  
local function pauseSprite ()  
 --PAUSE your sprite animation here (whatever code you're using for that)  
 timerRunning = false --reset flag because timer has stopped (thus, is now false)  
end  
  
--this function is called when the sprite begins animation (after a touch)  
local function startTimer ()   
 if ( timerRunning == false ) then  
 timer.performWithDelay( 2000, pauseSprite )  
 timerRunning = true  
 end  
end  

Give that a test and let me know if it works.

Brent
[import]uid: 9747 topic_id: 31297 reply_id: 125143[/import]

It works. Issue Resolved. Thanks a lot :slight_smile: [import]uid: 175611 topic_id: 31297 reply_id: 125218[/import]