How to restart a timer.performWithDelay function

Hi again,

In this case (a “pausing” timer), just pause bombTimer in the function “createBomb” using timer.pause(), then start another timer that executes a function to resume bombTimer after 10 seconds. You’ll want to assign this “restart” timer to another reference variable in case the user restarts the game while those 10 seconds are elapsing… in which case, you’d want to cancel that restart timer and the paused bombTimer.

So basically, this procedure:

  1. Start bombTimer (800 milliseconds, it appears to be set at).
  2. When the target function (createBomb) executes, pause bombTimer.
  3. Start a new timer (10 seconds) that executes a function like “resumeTimer()”
  4. When this function executes, resume bombTimer using timer.resume()

That should do it! If you still have problems with the timers not canceling properly, let me know because I have an alternative method which might work a bit better.

Brent
[import]uid: 9747 topic_id: 30328 reply_id: 121907[/import]

Hey Brent. So I tried your method but it was a little confusing and I was getting lost with my codes. What is the alternative method you had in mind that might work a little better? [import]uid: 69494 topic_id: 30328 reply_id: 123143[/import]

Hi Neil,

The alternative method really isn’t much more “simple”, it just does the same basic thing slightly differently. Where are you getting confused in your code?

Brent

[import]uid: 9747 topic_id: 30328 reply_id: 123351[/import]

Well I guess it’s sort of like sebitttas’s example but is this a separate function or do I have to put this in the spawnBomb function? It was a little confusing where to put everything in the storyboard such as variables like timer = 0 etc. I tried to put timer.pause(bombTimer) in the pause function but it wasn’t reading it, which was strange. Do you have a better example?

[lua]if timer <= 3 then
timer.pause(bombTimer1)
end
if timer >=10 and timer < 20 then
timer.pause (bombTimer)
timer.resume(bombTimer1)
end
if timer >= 20 and timer < 40 then
timer.pause(bombTimer1)
timer.resume(bombTimer)
end [lua] [import]uid: 69494 topic_id: 30328 reply_id: 123401[/import]

Hi Neil,
OK, let me try to put the method into code… but outside Storyboard… I actually don’t use that (I use a drastically trimmed-down version of Director), so I can’t vouch for the behavior of Storyboard or how to implement variables across modules within it, etc.

Anyway, from a “base code” standpoint, it might look like this:

local bombTimer  
local currentBombs = 0 --counter to store number of bombs created in the cycle  
local maxBombs = 1 --counter to dictate max # of bombs on the screen  
  
local function createBomb()  
  
 timer.cancel( bombTimer ) --cancel the timer (probably not necessary, but safer)  
 --  
 -- CREATE a new bomb here  
 --  
 currentBombs = currentBombs+1 --increment the counter  
  
 if ( currentBombs \< maxBombs ) then --if max bombs is not reached...  
 bombTimer = timer.performWithDelay( 800, createBomb, 1 ) --queue another bomb at 800 ms  
 else --ELSE, max bombs is reached, so pause 10 full seconds  
 currentBombs = 0 --reset counter  
 maxBombs = 8 --change if you want a different value on "next maximum"  
 bombTimer = timer.performWithDelay( 10000, createBomb, 1 )  
 end  
  
end  
  
local function startGame()  
 maxBombs = 5 --set 5 as the limit of bombs before the long pause  
 bombTimer = timer.performWithDelay( 800, createBomb, 1 )  
end  

Give that a try and see if it works! Again, I can’t vouch for functionality within Storyboard… you’ll have to figure that part out. :slight_smile:

Brent
[import]uid: 9747 topic_id: 30328 reply_id: 123416[/import]

Hey Brent. So I tried your method but it was a little confusing and I was getting lost with my codes. What is the alternative method you had in mind that might work a little better? [import]uid: 69494 topic_id: 30328 reply_id: 123143[/import]

Hi Neil,

The alternative method really isn’t much more “simple”, it just does the same basic thing slightly differently. Where are you getting confused in your code?

Brent

[import]uid: 9747 topic_id: 30328 reply_id: 123351[/import]

Well I guess it’s sort of like sebitttas’s example but is this a separate function or do I have to put this in the spawnBomb function? It was a little confusing where to put everything in the storyboard such as variables like timer = 0 etc. I tried to put timer.pause(bombTimer) in the pause function but it wasn’t reading it, which was strange. Do you have a better example?

[lua]if timer <= 3 then
timer.pause(bombTimer1)
end
if timer >=10 and timer < 20 then
timer.pause (bombTimer)
timer.resume(bombTimer1)
end
if timer >= 20 and timer < 40 then
timer.pause(bombTimer1)
timer.resume(bombTimer)
end [lua] [import]uid: 69494 topic_id: 30328 reply_id: 123401[/import]

Hi Neil,
OK, let me try to put the method into code… but outside Storyboard… I actually don’t use that (I use a drastically trimmed-down version of Director), so I can’t vouch for the behavior of Storyboard or how to implement variables across modules within it, etc.

Anyway, from a “base code” standpoint, it might look like this:

local bombTimer  
local currentBombs = 0 --counter to store number of bombs created in the cycle  
local maxBombs = 1 --counter to dictate max # of bombs on the screen  
  
local function createBomb()  
  
 timer.cancel( bombTimer ) --cancel the timer (probably not necessary, but safer)  
 --  
 -- CREATE a new bomb here  
 --  
 currentBombs = currentBombs+1 --increment the counter  
  
 if ( currentBombs \< maxBombs ) then --if max bombs is not reached...  
 bombTimer = timer.performWithDelay( 800, createBomb, 1 ) --queue another bomb at 800 ms  
 else --ELSE, max bombs is reached, so pause 10 full seconds  
 currentBombs = 0 --reset counter  
 maxBombs = 8 --change if you want a different value on "next maximum"  
 bombTimer = timer.performWithDelay( 10000, createBomb, 1 )  
 end  
  
end  
  
local function startGame()  
 maxBombs = 5 --set 5 as the limit of bombs before the long pause  
 bombTimer = timer.performWithDelay( 800, createBomb, 1 )  
end  

Give that a try and see if it works! Again, I can’t vouch for functionality within Storyboard… you’ll have to figure that part out. :slight_smile:

Brent
[import]uid: 9747 topic_id: 30328 reply_id: 123416[/import]