timer.cancel issues

I have my game set it when the time = 0 or the user has died to cancel the timer and move to the restart scene but I get an error with my timer.cancel.  Anyone have any idea what I’m doing wrong?

[lua]local timeLimit = 20
local count = 0

local function timerDown()
timeLimit = timeLimit-1
timeLeft.text = timeLimit
if(timeLimit==0)then
timer.cancel(timerDown)
storyboard.gotoScene(“restart”)
storyboard.purgeScene(“game”)
end
end

timer.performWithDelay(1000,timerDown,-1)

function endGame(event)
if event.phase == “began” then
timer.cancel(timerDown)
storyboard.gotoScene(“restart”)
storyboard.purgeScene(“game”)
end
return true
end[/lua]

You need to specify the handle of the timer to timer.cancel() not the function.

Something like this:

local timeLimit = 20 local count = 0 local timerHandle; local function timerDown() timeLimit = timeLimit-1 timeLeft.text = timeLimit if(timeLimit==0)then timer.cancel(timerHandle) storyboard.gotoScene("restart") storyboard.purgeScene("game") end end timerHandle = timer.performWithDelay(1000,timerDown,-1) function endGame(event) if event.phase == "began" then timer.cancel(timerHandle) storyboard.gotoScene("restart") storyboard.purgeScene("game") end return true end

when the game ends and I restart it the timer is at the last number it was at and doesn’t count down anymore and the score doesn’t reset.  shouldn’t everything reset once the scene is reloaded?

Try setting all default game variables/time-limit values/scores in your createScene event.

Ok that worked, but now when I restart the game for some reason my score adds 3 every time instead of just 1?

You need to specify the handle of the timer to timer.cancel() not the function.

Something like this:

local timeLimit = 20 local count = 0 local timerHandle; local function timerDown() timeLimit = timeLimit-1 timeLeft.text = timeLimit if(timeLimit==0)then timer.cancel(timerHandle) storyboard.gotoScene("restart") storyboard.purgeScene("game") end end timerHandle = timer.performWithDelay(1000,timerDown,-1) function endGame(event) if event.phase == "began" then timer.cancel(timerHandle) storyboard.gotoScene("restart") storyboard.purgeScene("game") end return true end

when the game ends and I restart it the timer is at the last number it was at and doesn’t count down anymore and the score doesn’t reset.  shouldn’t everything reset once the scene is reloaded?

Try setting all default game variables/time-limit values/scores in your createScene event.

Ok that worked, but now when I restart the game for some reason my score adds 3 every time instead of just 1?