Timer function changing it's callback timer

Okay, I register a timer that fires every 2 seconds to begin to spawn a bad guy.

timer.performWithDelay(timeOffset, spawnBox, 0)

Based on some logic inside spawnBox(), I want to call timer.cancel() and then immediately behind it call timer.performWithDelay(timeOffset-100, spawnBox, 0) but this second timer never starts up. What should I do to make it work right? Cancel one timer, immediately start another with a different delay. [import]uid: 75335 topic_id: 12329 reply_id: 312329[/import]

Cancelling it and starting one with a different delay would be a good way of going about it, yes. (Just be sure to always name your timers!) [import]uid: 52491 topic_id: 12329 reply_id: 44978[/import]

That doesn’t work at all unfortunately unless I’m doing something just horribly wrong. Let me show you my code and maybe since I’m a newbie you can help to explain where I’ve gone wrong :slight_smile:
[lua]local function spawnBox(event)
buttonCount = buttonCount + 1
activeButtons = activeButtons + 1
if activeButtons >= 5 then
local gameOverText = display.newText(“GAME OVER”, 50,200,“Georgia”, 54)
gameOver = true
timer.cancel(event.source)
end
if buttonCount % 5 == 0 then
if timeOffset > 200 then
timeOffset = timeOffset - 500
timer.cancel(event.source)
timer.performWithDelay(timeOffset, spawnBox, 0)
end
end
local button = display.newImage( “face_box.png” )
button:scale(2,2)
button.x = math.random(0+button.contentWidth/2,display.contentWidth-button.contentWidth/2)
button.y = math.random(0+button.contentHeight/2,display.contentHeight-button.contentHeight/2)
button:addEventListener(“touch”, touch)
end
timer.performWithDelay(timeOffset, spawnBox, 0)[/lua] [import]uid: 75335 topic_id: 12329 reply_id: 45013[/import]

Well, maybe I shouldn’t code when I’m sleepy or something? I just tested this code again this morning and it works exactly as expected…so, really not sure what was going on before! [import]uid: 75335 topic_id: 12329 reply_id: 45014[/import]

Hehe, glad it’s working now in any case. (I do the same thing sometimes.)

Peach :slight_smile: [import]uid: 52491 topic_id: 12329 reply_id: 45194[/import]