Stop a function (scoring module) on Collision?

Hey guys. I’m making a game. You play the game, as time passes your score builds, and when you hit something (lose the game) you go to the restart screen. What I want to do is be able to show the score on the restart screen, but I’m have some trouble because I can’t get the score to stop when you lose the game (when your jet, in this case, collides with something).

My code is something like this:

scoreTxt = display.newText( "Score: 0", .5,.5, "Helvetica", 20) scoreTxt:setFillColor(.5,0,.5) scoreTxt.AnchorX = 0 scoreTxt.x = display.screenOriginX + 450 scoreTxt.y = display.screenOriginY + 20 screenGroup:insert(scoreTxt) score = 0 function updateScore() score = score + 1 scoreTxt.text = score end timer.performWithDelay(1, updateScore, -1) function onCollision(event) if event.phase == "began" then storyboard.gotoScene("restart", "fade", 400) end end Runtime:addEventListener("collision", onCollision)

(I only picked the relevant pieces of code to copy and paste. And I also know the “Score:0” doesn’t actually do anything, but all I care about is having the numbers on the screen.)

How can I stop updateScore from continuing when something collides?

Thanks in advance!

-Tariq

Hi Tariq,

In this case, you need to cancel the repeating timer that is causing the score to update. This means you’ll also need to assign the timer to a variable (handle) so you can reference it.

http://docs.coronalabs.com/api/library/timer/cancel.html

Also, I suggest that you don’t fire the timer ever 1 millisecond… that’s a lot of updating and I think you could manage with something like 50 milliseconds.

Best regards,

Brent

Hi Tariq,

In this case, you need to cancel the repeating timer that is causing the score to update. This means you’ll also need to assign the timer to a variable (handle) so you can reference it.

http://docs.coronalabs.com/api/library/timer/cancel.html

Also, I suggest that you don’t fire the timer ever 1 millisecond… that’s a lot of updating and I think you could manage with something like 50 milliseconds.

Best regards,

Brent