I have a timer in my code that spawns objects every 1 seconds. When an object collides with "deathObj" the scene changes from the scene2 to the scene1. But when from the scene1 I return to the scene2 the timer is called twice, if I retry will be called 4 times etc... How can I call the timer once?
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
elseif ( phase == "did" ) then
local function spawn()
--things for spawn the object
end
end
local function deathObjCollision(self, event)
if (event.phase == "began" ) then
composer.gotoScene("scene1")
end
end
deathObj = display.newRect(300,1900,1600,100)
physics.addBody(deathObj, "static", {density=1.0, friction=0.5, bounce=0.3})
deathObj.collision = deathObjCollision
deathObj:addEventListener( "collision", deathObj )
spawnTimer = timer.performWithDelay(1000, spawn, -1)
end
end
Try to pause or cancel the timer when you change from scene 2 to scene 1.