I added a timer in my code but don’t know how to stop it and then restart it when the user restarts the game . I tried putting in the timer.cancel function but it’s not working I also tried to reset it in the restart file before it goes into to game and it didn’t work. I got the timer moving but I want it to stop and reset when the user starts the game over .
EDIT !!!
-- requires local ads = require "ads" local physics = require "physics" physics.start() local storyboard = require( "storyboard" ) local scene = storyboard.newScene() -- background function scene:createScene(event) local screenGroup = self.view local background = display.newImageRect("bg.png",display.contentWidth,display.contentHeight) background.x = display.contentCenterX background.y = display.contentCenterY screenGroup:insert(background) ceiling = display.newImage("invisibleTile.png") ceiling.x = 0 ceiling.y = -33 physics.addBody(ceiling, "static", {density=.1, bounce=0.2, friction=.2}) screenGroup:insert(ceiling) theFloor = display.newImage("invisibleTile.png") theFloor.x = 0 theFloor.y = 347 physics.addBody(theFloor, "static", {density=.1, bounce=0.2, friction=.2}) screenGroup:insert(theFloor) city1 = display.newImage("city1.png") city1.x = 240 city1.y = 210 city1.speed = 2 screenGroup:insert(city1) city2 = display.newImage("city1.png") city2.x = 240 city2.y = 270 city2.speed = 2 screenGroup:insert(city2) city3 = display.newImage("city2.png") city3.x = 240 city3.y = 270 city3.speed = 3 screenGroup:insert(city3) city4 = display.newImage("city2.png") city4.x = 240 city4.y = 270 city4.speed = 3 screenGroup:insert(city4) jet = display.newImage("redJet.png") jet.x = 100 jet.y = 100 physics.addBody(jet, "dynamic", {density=.1, bounce=0.1, friction=.2, radius=12}) screenGroup:insert(jet) mine1 = display.newImage("mine.png") mine1.x = 500 mine1.y = 100 mine1.speed = math.random(4,8) mine1.initY = mine1.y mine1.amp = math.random(20, 140) mine1.angle = math.random(1, 360) physics.addBody(mine1, "static", {density=.1, bounce=0.2, friction=.2, radius=12}) screenGroup:insert(mine1) mine2 = display.newImage("mine.png") mine2.x = 500 mine2.y = 100 mine2.speed = math.random(4,8) mine2.initY = mine2.y mine2.amp = math.random(20, 140) mine2.angle = math.random(1, 360) physics.addBody(mine2, "static", {density=.1, bounce=0.2, friction=.2, radius=12}) screenGroup:insert(mine2) mine3 = display.newImage("mine.png") mine3.x = 500 mine3.y = 100 mine3.speed = math.random(4,8) mine3.initY = mine3.y mine3.amp = math.random(20, 150) mine3.angle = math.random(1, 360) physics.addBody(mine3, "static", {density=.1, bounce=0.2, friction=.2, radius=12}) screenGroup:insert(mine3) mine4 = display.newImage("mine.png") mine4.x = 500 mine4.y = 100 mine4.speed = math.random(2,10) mine4.initY = mine4.y mine4.amp = math.random(20, 100) mine4.angle = math.random(1, 360) physics.addBody(mine4, "static", {density=.1, bounce=0.2, friction=.2, radius=12}) screenGroup:insert(mine4) mine5 = display.newImage("mine.png") mine5.x = 500 mine5.y = 100 mine5.speed = math.random(2,4) mine5.initY = mine4.y mine5.amp = math.random(20, 130) mine5.angle = math.random(1, 360) physics.addBody(mine5, "static", {density=.1, bounce=0.2, friction=.2, radius=12}) screenGroup:insert(mine5) end function scrollCity( self, event ) if self.x \< -200 then self.x = 200 else self.x = self.x - self.speed end end function moveMines( self, event ) if self.x \< -50 then self.x = 500 self.y = math.random(90, 220) self.speed = math.random(4,8) self.initY = mine1.y self.amp = math.random(20, 100) self.angle = math.random(1, 360) else self.x = self.x - self.speed self.angle = self.angle + .1 self.y = self.amp\*math.sin(self.angle)+self.initY end end -- jet function activateJets( self, event ) self:applyForce(0, -4.7, self.x, self.y) end function touchScreen( event ) if event.phase == "began" then jet.enterFrame = activateJets Runtime: addEventListener("enterFrame", jet) end if event.phase == "ended" then Runtime: removeEventListener("enterFrame", jet) end end --this must NOT be local to the start game function, as you will need to access it in the exit game function local startTime --create a text object to display the timer local myTimeText = display.newText("0", 50, 50, native.systemFont, 20) local function startGame() --get time that the game started startTime = system.getTimer() end local function update() --get the current time local now = system.getTimer() --calculate time in milliseconds spent in game local timeSpentInGame = now - startTime --convert to seconds if that is useful for you local secondsInGame = timeSpentInGame / 1000 --update the text object to show the number of seconds myTimeText.text = secondsInGame end startGame() function onCollision( event ) if event.phase =="began" then storyboard.gotoScene("restart", "fade", 400) end end function scene:enterScene(event) Runtime:addEventListener("enterFrame", update) city1.enterFrame = scrollCity Runtime:addEventListener("enterFrame", city1) city2.enterFrame = scrollCity Runtime:addEventListener("enterFrame", city2) city3.enterFrame = scrollCity Runtime:addEventListener("enterFrame", city3) city4.enterFrame = scrollCity Runtime:addEventListener("enterFrame", city4) mine1.enterFrame = moveMines Runtime:addEventListener("enterFrame", mine1) mine2.enterFrame = moveMines Runtime:addEventListener("enterFrame", mine2) mine3.enterFrame = moveMines Runtime:addEventListener("enterFrame", mine3) mine4.enterFrame = moveMines Runtime:addEventListener("enterFrame", mine4) mine5.enterFrame = moveMines Runtime:addEventListener("enterFrame", mine5) Runtime:addEventListener("touch", touchScreen) Runtime:addEventListener("collision", onCollision) end function scene:exitScene(event) Runtime:removeEventListener("touch", touchScreen) Runtime:removeEventListener("enterFrame", city1) Runtime:removeEventListener("enterFrame", city2) Runtime:removeEventListener("enterFrame", city3) Runtime:removeEventListener("enterFrame", city4) Runtime:removeEventListener("enterFrame", mine1) Runtime:removeEventListener("enterFrame", mine2) Runtime:removeEventListener("enterFrame", mine3) Runtime:removeEventListener("enterFrame", mine4) Runtime:removeEventListener("enterFrame", mine5) Runtime:removeEventListener("enterFrame", startTime) Runtime:removeEventListener("collision", onCollision) end function scene:destroyScene(event) end scene:addEventListener("createScene", scene) scene:addEventListener("enterScene", scene) scene:addEventListener("exitScene", scene) scene:addEventListener("destroyScene", scene) return scene