Timer error

I am trying to put a timer in my app but i am getting this error .

This is my code. 

-- 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 output = display.newText("0", 0, 0, native.systemFont, 32) output:setTextColor(255, 255, 255); output.x = \_W \* 0.5; output.y = \_H \* 20; stateText = display.newText("You Beat The Game . Congartulations !", 0, 0, native.systemFont, 16) output:setTextColor(255, 255, 255); output.x = \_W \* 0.5; output.y = \_H \* 20; addSetText(output); addSetText(stateText); local tmr; tmr = timer.performWithDelay(500, function(e) output:stateText(e.count); if(e.count == 100000) then timer.cancel(tmr); tmr = nil; stateText:setText("You beat the game"); end end,100000); 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, -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 onCollision( event ) if event.phase =="began" then storyboard.gotoScene("restart", "fade", 400) 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

I want the timer to go up by one and when the user dies it stops and when they restart the game the timer starts over .

At line 130, you’re trying to use a variable named _W which doesn’t exist.  Its likely you declared it local in main.lua when you intended for it to be global.

Rob

Now i’m getting this 

game.lua: 142 attempt to call global ‘addsetText’ (a nil value)

I just put this code but how do i stop it when the user dies ?

\_W = display.contentWidth \_H = display.contentHeight number = 0 local txt\_counter = display.newText( number, 0, 0, native.systemFont, 20 ) txt\_counter.x = \_W/5 txt\_counter.y = \_H/6 txt\_counter:setTextColor( 255, 255, 255 ) function fn\_counter() number = number + 1 txt\_counter.text = number end timer.performWithDelay(1000, fn\_counter, 0)

I don’t see where you create a function called addSetText(). I see where you call it, but not where you define it.

addSetText(output); addSetText(stateText);

Also, this code (including the above)

output = display.newText("0", 0, 0, native.systemFont, 32) output:setTextColor(255, 255, 255); output.x = \_W \* 0.5; output.y = \_H \* 20; stateText = display.newText("You Beat The Game . Congartulations !", 0, 0, native.systemFont, 16) output:setTextColor(255, 255, 255); output.x = \_W \* 0.5; output.y = \_H \* 20; addSetText(output); addSetText(stateText); local tmr; tmr = timer.performWithDelay(500, function(e) output:stateText(e.count); if(e.count == 100000) then timer.cancel(tmr); tmr = nil; stateText:setText("You beat the game"); end end,100000);

lives in the scene’s main chunk. That means it will execute only when the scene is first required. this will happen before scene:create() runs, it will run before scene:show() runs. The two display objects you’re creating are not being managed by Composer. They will stay on the screen unless you explicitly remove them and more importantly, this code will not re-execute the next time the scene loads unless you are completely removing the scene.

As for your second question, that’s really a different topic, but basically  you are running the timer without capturing it’s handle. You won’t be able to cancel it later. Save the handle by doing:

local fnCounter\_timer = timer.performWithDelay(1000, fn\_counter, 0)

Then when you are ready to stop it, do: 

timer.cancel( fnCounter\_timer )

Rob

It stops but when the user goes back into the game , it doesn’t start . I tried using the timer.resume function but it doesn’t work

Your timer’s should be defined in your scene:show() function. That way when you enter the scene they will be started after the scene is on the screen.

Rob

Actually I want the timer to reset when the user restarts 

Then create a new timer. scene:show() runs twice every time the scene loads, once before the scene comes on the screen (event.phase == “will”) and once after it’s on the screen (event.phase == “did”)

So if you define your timer handle variable at the top of your scene in the main chunk:

local myTimerHandle

Then in the function scene:show()

function scene:show( event ) &nbsp;&nbsp;&nbsp;&nbsp; local sceneGroup = self.view &nbsp;&nbsp;&nbsp;&nbsp; if event.phase == "did" then -- wait until the scene is on screen &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myTimerHandle = timer.performWithDelay( 1000, fn\_counter, 0 ) &nbsp;&nbsp;&nbsp; end end

Then just before you call composer.gotoScene() to leave this scene call:

timer.cancel( myTimerHandle )

It doesn’t work

I don’t want to seem mean here, but do you realize how un-helpful " It doesn’t work" is?

What doesn’t work?

How is it not working?

What are you expecting it to do?

What are you observing happening?

Are you getting any error messages in your console.log?

While we are on the subject of how to ask for help, you have asked two questions in this thread with two blocks of code that don’t appear to go together. I don’t know if " It doesn’t work" is referring to the original question or the second smaller timer block? I have no context in how you’re trying to that timer block since it’s not part of your original code.

What I provided is probably not usable code. I have to make up example variable names. I have to assume where you’re going to use it. I have no idea how you actually implemented the code. You didn’t provide your modified code.

Please, help us help you. Take time and ask good questions. Provide the information we need to help you.

Consider this a fair warning, don’t expect responses to “It doesn’t work”.

Rob

I want the timer to count the seconds the user is in the game , and it works . When they die I want the timer to stop , that works too but I want the timer to reset when the user restarts the game because all it does is stay on the number that was previouslyg given

You are still not providing enough information.

Are you getting errors?

How about a screens shot?

Post some related code?

We want you to be successful, but you have to give us something to go on.

I’m not getting any errors , it just doesn’t work . I took the code out already because I seen that it wasn’t working for me 

At line 130, you’re trying to use a variable named _W which doesn’t exist.  Its likely you declared it local in main.lua when you intended for it to be global.

Rob

Now i’m getting this 

game.lua: 142 attempt to call global ‘addsetText’ (a nil value)

I just put this code but how do i stop it when the user dies ?

\_W = display.contentWidth \_H = display.contentHeight number = 0 local txt\_counter = display.newText( number, 0, 0, native.systemFont, 20 ) txt\_counter.x = \_W/5 txt\_counter.y = \_H/6 txt\_counter:setTextColor( 255, 255, 255 ) function fn\_counter() number = number + 1 txt\_counter.text = number end timer.performWithDelay(1000, fn\_counter, 0)

I don’t see where you create a function called addSetText(). I see where you call it, but not where you define it.

addSetText(output); addSetText(stateText);

Also, this code (including the above)

output = display.newText("0", 0, 0, native.systemFont, 32) output:setTextColor(255, 255, 255); output.x = \_W \* 0.5; output.y = \_H \* 20; stateText = display.newText("You Beat The Game . Congartulations !", 0, 0, native.systemFont, 16) output:setTextColor(255, 255, 255); output.x = \_W \* 0.5; output.y = \_H \* 20; addSetText(output); addSetText(stateText); local tmr; tmr = timer.performWithDelay(500, function(e) output:stateText(e.count); if(e.count == 100000) then timer.cancel(tmr); tmr = nil; stateText:setText("You beat the game"); end end,100000);

lives in the scene’s main chunk. That means it will execute only when the scene is first required. this will happen before scene:create() runs, it will run before scene:show() runs. The two display objects you’re creating are not being managed by Composer. They will stay on the screen unless you explicitly remove them and more importantly, this code will not re-execute the next time the scene loads unless you are completely removing the scene.

As for your second question, that’s really a different topic, but basically  you are running the timer without capturing it’s handle. You won’t be able to cancel it later. Save the handle by doing:

local fnCounter\_timer = timer.performWithDelay(1000, fn\_counter, 0)

Then when you are ready to stop it, do: 

timer.cancel( fnCounter\_timer )

Rob

It stops but when the user goes back into the game , it doesn’t start . I tried using the timer.resume function but it doesn’t work

Your timer’s should be defined in your scene:show() function. That way when you enter the scene they will be started after the scene is on the screen.

Rob