benLIVE
benLIVE

Reputation: 597

Corona: How to remove a timer from the exitScene function?

I need to remove the timer from the exitScene function as I remove all the listeners but I didn't found how. This is the code:

function scene:enterScene(event)

    planeta.enterFrame = rotarPlaneta
    Runtime:addEventListener("enterFrame", planeta)

    Runtime:addEventListener("touch", touchScreen)

    timer.performWithDelay( 1000, throwBrickEnemigo,0 )
end

function scene:exitScene(event)

    Runtime:removeEventListener("enterFrame", planeta)
    Runtime:removeEventListener("enterFrame", touchScreen)
    Runtime:removeEventListener("enterFrame", planeta)

end

Upvotes: 1

Views: 3409

Answers (1)

dburdan
dburdan

Reputation: 459

You need to assign a variable to the timer and cancel it.

local throwBrickTimer -- Reference for the timer

function scene:enterScene(event)

    planeta.enterFrame = rotarPlaneta
    Runtime:addEventListener("enterFrame", planeta)

    Runtime:addEventListener("touch", touchScreen)

    --> Give the timer a variable
    throwBrickTimer = timer.performWithDelay( 1000, throwBrickEnemigo,0 ) 
end

function scene:exitScene(event)

    --> Cancel the timer
    timer.cancel(throwBrickTimer)

    Runtime:removeEventListener("enterFrame", planeta)
    Runtime:removeEventListener("enterFrame", touchScreen)
    Runtime:removeEventListener("enterFrame", planeta)

end

Upvotes: 4

Related Questions