I put a timer in my app that goes by ones and it works how I want it to but , when the user dies and restarts the game , the timer is frozen at the number that the user died on . Does anybody know how I can make it reset when the user restarts the game ?
Are you using composer cause if so you could make it so when you change scenes it makes timer 0 so you would write timer= 0 in the section where you change scenes. Ps. i have not tested this oh.i fogot to add that before you call the code above you do timer.cancel
I’m usings storyboard
It should still work actually. Just try it out.
No it doesn’t work . I tried putting it where the game goes into the restart screen it didn’t work , then i tried putting it where the restart goes back into the screen and it still didn’t work
Simply just remove the timer and then re add it.
local yourTimer timer.cancel(yourTimer) yourTimer = timer.performWithDelay( timeHere, functionHere, howMuchItRepeatsHere )
Good Luck!
–SonicX278
It doesn’t work
“It doesn’t work” won’t help us solve your problem or help atleast. Can you post some code?
–SonicX278
game.lua
-- requires local ads = require "ads" local physics = require "physics" physics.start() local storyboard = require( "storyboard" ) local scene = storyboard.newScene() -- background function scene:createScene(event) ads.hide() 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 = -29 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(2,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,6) 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 = 140 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 = 90 mine4.speed = math.random(2,10) mine4.initY = mine4.y mine4.amp = math.random(20, 100) mine4.angle = math.random(1, 260) 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 = 50 mine5.speed = math.random(2,6) mine5.initY = mine5.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) mine6 = display.newImage("mine.png") mine6.x = 150 mine6.y = 450 mine6.speed = math.random(2,8) mine6.initY = mine6.y mine6.amp = math.random(20, 130) mine6.angle = math.random(1, 360) physics.addBody(mine6, "static", {density=.1, bounce=0.2, friction=.2, radius=12}) screenGroup:insert(mine6) end \_W = display.contentWidth \_H = display.contentHeight number = 0 local txt\_counter = display.newText( number, 0, 0, native.systemFont, 20 ) txt\_counter.x = \_W/7 txt\_counter.y = \_H/7 txt\_counter:setTextColor( 255, 255, 255 ) function fn\_counter() number = number + 1 txt\_counter.text = number end local fnCounter\_timer = timer.performWithDelay(1000, fn\_counter, 0) function scrollCity( self, event ) if self.x \< -200 then self.x = 200 else self.x = self.x - self.speed end end function onCollision( event ) if event.phase =="began" then storyboard.gotoScene("restart", "fade", 400) timer.cancel( fnCounter\_timer ) 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, -3.5, 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 function scene:enterScene(event) 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) mine6.enterFrame = moveMines Runtime:addEventListener("enterFrame", mine6) 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", mine6) 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
restart.lua
– requires
local ads = require “ads”
local storyboard = require( “storyboard” )
local scene = storyboard.newScene()
– background
local function adListener( event )
if ( event.isError ) then
end
end
ads.init( “admob”, “ca-app-pub-8392055366133178/5358470847”, adListener )
ads.show( “banner”, { x=0, y=0, appId=“ca-app-pub-8392055366133178/5358470847” } )
local function adListener( event )
if ( event.isError ) then
end
end
ads.init( “admob”, “ca-app-pub-8392055366133178/9559841245”, adListener )
ads.show( “interstitial”, { x=0, y=0, appId=“ca-app-pub-8392055366133178/9559841245” } )
function scene:createScene(event)
local screenGroup = self.view
background = display.newImageRect(“restart.png”,display.contentWidth,display.contentHeight)
background.x = display.contentCenterX
background.y = display.contentCenterY
screenGroup:insert(background)
city2 = display.newImage(“city1.png”)
city2.x = 230
city2.y = 220
city2.speed = 2
screenGroup:insert(city2)
end
function start( event )
if event.phase == “began” then
storyboard.gotoScene(“game”, “fade”, 400)
fnCounter_timer = timer.performWithDelay()
end
end
function scene:enterScene(event)
storyboard.purgeScene(“game”)
background:addEventListener(“touch”, start)
end
function scene:exitScene(event)
background:removeEventListener(“touch”, start)
end
function scene:destroyScene(event)
end
scene:addEventListener(“createScene”, scene)
scene:addEventListener(“enterScene”, scene)
scene:addEventListener(“exitScene”, scene)
scene:addEventListener(“destroyScene”, scene)
return scene
That’s to much to look through as in on my phone. Can you cut it down to the basics.?
–SonicX278
the timer in my game.lua file .
\_W = display.contentWidth \_H = display.contentHeight number = 0 local txt\_counter = display.newText( number, 0, 0, native.systemFont, 20 ) txt\_counter.x = \_W/7 txt\_counter.y = \_H/7 txt\_counter:setTextColor( 255, 255, 255 ) function fn\_counter() number = number + 1 txt\_counter.text = number end local fnCounter\_timer = timer.performWithDelay(1000, fn\_counter, 0) function onCollision( event ) if event.phase =="began" then storyboard.gotoScene("restart", "fade", 400) timer.cancel( fnCounter\_timer ) end end
timer reset in restart.lua file
function start( event ) if event.phase == "began" then storyboard.gotoScene("game", "fade", 400) fnCounter\_timer = timer.performWithDelay() end end
You can’t do that. You can’t add a local variable in one file and try to cancel it in another… What you could do is chekc it timer is nil and then make a new time in the game.lua.
I want the timer to stop when the user dies and then reset when the user restarts the game
Exactly.
–SonicX278
Yes .
I dont know if i mentioned this but in scene destroy try adding local number = 0 and the timer.cancel
That doesn’t work
function scene:destroyScene(event) local number = 0 timer.cancel( fnCounter\_timer ) end
That’s what I put
Ok so just do this.
local score = 0 local scoreTxt = display.newText( "0", 0, 0, native.systemFontBold, 40 ) scoreTxt.x = display.contentCenterX scoreTxt.y = 20 sceneGroup:insert(scoreTxt) local function updateScore() score = score + 1 scoreTxt.text = string.format("%d", score ) end local scoreTimer = timer.performWithDelay(1000, updateScore, -1)
So i’m using a string here that gets updated every second… And that shows the score… So when you are going to your next scene you do this.
local function gotoNext() composer.gotoScene( "bob" ) display.remove(scoreTxt) timer.cancel( scoreTimer ) end
Now when you start the game again then the score will be 0 and then timer will start like normal.
MAKE SURE you are in scope! I don’t want to here that this isn’t working because i use this in every game i make. Not trying to sound rude or police you but this is the simplest way i could give you to do it.
–SonicX278
I would cancel the timer BEFORE storyboard.gotoScene and don’t forget to nil the reference to it.
local function onCollision( event ) if event.phase =="began" then timer.cancel( fnCounter\_timer ) fnCounter\_timer = nil storyboard.gotoScene("restart", "fade", 400) end end
Ahh yes! Sorry I missed that part.
–SonicX278