Timer doesn't work properly

Hi; m question is about timer. i can use timer but if a have secand scene to show something( it includes score and play agian button) , pushing play again going back scene2.lua from scene3.lua. Timer doesn’t work again. My codes below (scene2.lua)

local composer = require( "composer" ) local scene = composer.newScene() local tapCount = 0 local timeLimit = 5 local currentscore =0 -- Later... local background = display.newImageRect( "background.png", 360, 570 ) background.x = display.contentCenterX background.y = display.contentCenterY local platform = display.newImageRect( "platform.png", 300, 50 ) platform.x = display.contentCenterX platform.y = display.contentHeight-25 platform.myName="platform" local balloon = display.newImageRect( "balloon.png", 112, 112 ) balloon.x = display.contentCenterX balloon.y = display.contentCenterY balloon.myName="balloon" balloon.alpha = 0.8 local tapText = display.newText( tapCount, display.contentCenterX, 20, native.systemFont, 40 ) tapText:setFillColor( 0, 0, 0 ) timeLeft = display.newText("Time", display.contentCenterX+100,display.contentCenterY-250,native.systemFont, 20) timeLeft:setTextColor(255,0,0) function scene:create( event ) local sceneGroup = self.view sceneGroup:insert(background) sceneGroup:insert(platform) sceneGroup:insert(balloon) sceneGroup:insert(tapText) sceneGroup:insert(timeLeft) local textScene2 = display.newText("scene2", 50, 50, native.systemFont, 12) sceneGroup:insert(textScene2) textScene2:addEventListener("tap", nextScene) end timerSpawn = timer.performWithDelay(1000,timerDown,timeLimit) --PUSH BALLOON local function pushBalloon() balloon:applyLinearImpulse( 0, -0.75, balloon.x, balloon.y ) tapCount = tapCount + 1 tapText.text = tapCount print("%s","%d", "currenscore: ", tapCount) end -- COLLUSION local function onCollision( event ) if(event.object2.myName == "balloon" and event.object1.myName == "platform") then if(tapCount\>0) then tapCount = tapCount -1 tapText.text = tapCount end end end --TIMERDOWN local function timerDown() print("timer is working") timeLimit = timeLimit-1 timeLeft.text = timeLimit if(timeLimit==0)then --timer.cancel( timerSpawn ) timer.pause( timerSpawn ) currentscore = currentscore + tapCount composer.gotoScene( "scene3", { effect = "fade", time = 800, params = { level="Level 1", score=currentscore } } ) end end timerSpawn = timer.performWithDelay(1000,timerDown,timeLimit) local physics = require( "physics" ) physics.start() physics.addBody( platform, "static" ) physics.addBody( balloon, "dynamic", { radius=50, bounce=0.3 } ) balloon:addEventListener( "tap", pushBalloon ) Runtime:addEventListener( "collision", onCollision ) function nextScene(event) composer.gotoScene("scene3") end function scene:show( event ) local textScene2 = display.newText("scene2", 50, 100, native.systemFont, 12) local sceneGroup = self.view sceneGroup:insert(textScene2) timer.resume( timerSpawn ) -- timerSpawn = timer.performWithDelay(1000,timerDown,timeLimit) --timer.performWithDelay(1000,timerDown,timeLimit) end function scene:hide( event ) end function scene:destroy( event ) end scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) return scene

my question is about timer.i have 2 scenes(scene2.lua,scene3.lua). scene2 has timer that is working good when first appearance.After time ellapsed i am showing scene3 to show score.Scene3 has pkay agian button to play game agian by forwarding to scene2.Timer doesn’t work again. My codes below (scene2.lua)

Are you removing your scene2 before you go to it from scene3 by calling:

composer.removeScene("scene2")

???

Rob

Yes timer is worked. By using “composer.removeScene(“scene2”)” BUT this time, my physics not nowking properly and second time play again not working.

By the way; if you remove scene2, you can not ga again by pushing “playagain”. My entirecode is below :

(PLEASE RUN MY CODE.While deciding to write here.PLEASE

scene2->scene1.lua

scene3->scene2.lua

scene2.lua:

local composer = require( "composer" ) local scene = composer.newScene() local tapCount = 0 local timeLimit = 5 local currentscore =0 -- Later... local sceneGroup local timerDown local function timerDown( event ) print("timer çalışıyor") timeLimit = timeLimit-1 timeLeft.text = timeLimit if(timeLimit==0)then timer.pause( event.source ) currentscore = currentscore + tapCount composer.gotoScene( "scene2", { effect = "fade", time = 800, params = { level="Level 1", score=currentscore } } ) end end local background = display.newImageRect( "background.png", 360, 570 ) background.x = display.contentCenterX background.y = display.contentCenterY local platform = display.newImageRect( "platform.png", 300, 50 ) platform.x = display.contentCenterX platform.y = display.contentHeight-25 platform.myName="platform" local balloon = display.newImageRect( "balloon.png", 112, 112 ) balloon.x = display.contentCenterX balloon.y = display.contentCenterY balloon.myName="balloon" balloon.alpha = 0.8 local tapText = display.newText( tapCount, display.contentCenterX, 20, native.systemFont, 40 ) tapText:setFillColor( 0, 0, 0 ) timeLeft = display.newText("Time", display.contentCenterX+100,display.contentCenterY-250,native.systemFont, 20) timeLeft:setTextColor(255,0,0) local physics = require( "physics" ) local function pushBalloon() balloon:applyLinearImpulse( 0, -0.75, balloon.x, balloon.y ) tapCount = tapCount + 1 tapText.text = tapCount end local function onCollision( event ) if(event.object2.myName == "balloon" and event.object1.myName == "platform") then if(tapCount\>0) then tapCount = tapCount -1 tapText.text = tapCount end end end balloon:addEventListener( "tap", pushBalloon ) Runtime:addEventListener( "collision", onCollision ) function scene:create( event ) sceneGroup = self.view print("working create") end function scene:show( event ) timerDown = timer.performWithDelay(1000,timerDown,timeLimit) local sceneGroup = self.view local phase = event.phase print("working show") if ( phase == "will" ) then sceneGroup:insert(background) sceneGroup:insert(platform) sceneGroup:insert(balloon) sceneGroup:insert(tapText) sceneGroup:insert(timeLeft) physics.start() physics.addBody( platform, "static" ) physics.addBody( balloon, "dynamic", { radius=50, bounce=0.3 } ) elseif ( phase == "did" ) then end end function scene:hide( event ) end function scene:destroy( event ) end scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) return scene

scene2.lua:

local composer = require( "composer" ) local scene = composer.newScene() local sceneGroup local background = display.newImageRect( "background.png", 360, 570 ) background.x = display.contentCenterX background.y = display.contentCenterY local widget = require("widget") local bottomTabButtons ={ { width=50, height=32, defaultFile="play.png", overFile="playhover.png", onPress=function() composer.gotoScene( "scene1" ) end}, { width=50, height=32, defaultFile="quit.png", overFile="quithover.png", onPress=function() os.exit() end}, } local bottomBar = widget.newTabBar{ top = display.contentHeight-40, buttons = bottomTabButtons } function scene:create( event ) sceneGroup = self.view local params = event.params local myText1 = display.newText( params.score, display.contentCenterX, display.contentCenterY+50, native.systemFont, 48 ) local myText2 = display.newText( "Your Score !", display.contentCenterX, display.contentCenterY, native.systemFont, 48 ) myText1:setFillColor( 1, 0, 0 ) myText2:setFillColor( 1, 0, 0 ) sceneGroup:insert(background) sceneGroup:insert(myText1) sceneGroup:insert(myText2) sceneGroup:insert(bottomBar) end function scene:show( event ) if ( phase == "will" ) then composer.removeScene("scene1") elseif ( phase == "did" ) then end end function scene:hide( event ) end function scene:destroy( event ) end scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) return scene

We can’t run your code. We don’t have the complete project with all of the images… But what would be more helpful is to post any errors you’re getting in your console log (or warnings).

Also, when you ask the community to run your project for you and fix it, you’re asking them to do the work for you and you really won’t learn from it. Programming is challenging. Debugging can be frustrating. But when you do learn and figure it out, it’s such a great feeling.

Now back to helping  you. The code above doesn’t seem to be right. You posted two “scene2.lua” files. The one I think  you expect to be scene3.lua, I don’t see where you are removing scene2.

On a side note, you should not use os.exit(). You should instead use native.requestExit(). iOS apps should never exit like this and Apple will reject them. On Android some users expect apps to exit, but it’s generally not a needed feature. But you should use native.requestExit() instead.

Rob

Rob;

i don’t want any body to improve my project . But you are not looking for RemoveScene method also you are not runnig my code before writing your idea. how do you understand my problem. if you look at my code you can see easly my RemoveScene above.i have 12 years experience as C# developer. i know how to write program and i know any aspect of programmnig (Challenging debugging bla bla bla …) Please don’t waste my time off by writing weird ( not helping) comments . are they useful ? ( i guess “no” because not only me lots of people will have the same headache of it) i know something a button should work always. clicking once “great” secand click it  is messed up. Please be helpful.   

That’s a great way to get people to help you :smiley:

Rob might be a Corona staff member but a certain level of politeness and appreciation would go a long way, instead of accusing him of wasting your time.

Hi; 

Nick, do you have any empathy? Or You have lots of time for writing pointless sentences. Or you like this kinda behaviour of it by writing  “That’s a great way to get people to help you bla bla …” . Please be serious because you are really helpful to loose my view angle of corona sdk.  My question is above.

@yusufkaratoprak, everyone here really wants to be helpful. I know I personally want you to succeed. Sometimes people ask for help and perhaps language barriers get in the way. I’m willing to do what I can to help you with in reasonable constraints.  Let me try to address a couple of these.

First and foremost you want people to run your code but it’s impossible for me to do so. I do not have your main.lua, config.lua, build.settings and all of your scene files. You’ve only posted a couple of them above. I also don’t have any images or sounds that your app may be using. No one here can “make up” the missing pieces.

In 95%+ of the time, we don’t need to run your code to debug it. Part of debugging is recognizing a problem by the results you get. It’s not foolproof by any means, but it works quite well. We also can read and trace code and follow the logic. If we see something out of place, then there is a good chance we can diagnose and offer a suggestion based on the code alone.  Sometimes it helps to see screen shots of the problem or potentially a video displaying the problem.

If you want to bundle up your app and put it in a .zip file and share it, then we could run your app and see what the problem is. Many developers don’t want to share their app, so we have to fall back to screen shots, videos and descriptions. When we are limited to descriptions the better the description the more help it will be.

Now you posted two blocks of code. You named both of them in the post “scene2.lua”, so I don’t know what is really scene2.lua and what is either scene1.lua or scene3.lua, which you indicated also exist. But I think I’ve spotted the problem and the advice to call composer.removeScene(“scene2”) would certainly remove your buttons.

It appears that you’re using a widget.newTabBar() for your buttons. Are you wanting those buttons to always be on the screen and visible? I think you do. The rest of this will be based on that assumption.

When you are constructing a user interface (UI) that uses tabBars at the bottom and perhaps a title bar at the top, you generally want those around regardless of what makes up the rest of the scene. When this is a case, you do not want these to be a) part of a scene and b) managed by composer.

Tabbars are best created in main.lua or a non-Composer module that’s ran as part of main.lua. Objects that are not part of a scene sit on top of the display in what we call HUD or Heads Up Display mode. They will always be on top and always visible.

Consider moving your tabbar code to main.lua and see if that makes things work more like you think it will.

Rob

Hi;

i prepared my project as ZIP. there are 2 issues. one issue.

scene0.lua has one play button. (ok)

scene1.lua has platform,balloon and jumping physics.

scene2.lua has play button (PLay again) and score.

first issue is after playbutton of scene2 by clicking physics in scene1 is not working properly.

second isue is play again not working in second game playing. i mean i can not playgame more than twice.

i added my project as ZIP. Click below link.

https://upterabit.com/6sl/corona_MultiScreen.zip

Basically you are not removing scenes before you go to this. This is an odd concept. But you a) cannot remove the scene you are currently in and b) if you don’t remove them anything in the scene’s main chunk won’t run a second time. Most of your object creation is done in the main chunk, starting physics etc. With out removing the scene these things won’t happen again. Any way fix #1:

In scene2.lua in your code that handles the tabBar buttons, you need to remove scene1 before returning to scene1:

local bottomTabButtons ={   { width=50, height=32, defaultFile="play.png", overFile="playhover.png", onPress=function() composer.removeScene("scene1"); composer.gotoScene( "scene1" )  end},   { width=50, height=32, defaultFile="quit.png", overFile="quithover.png", onPress=function() os.exit() end}, }

Then in scene1.lua, you need to remove scene2 before you go to it:

local function timerDown( event ) print("timer çalışıyor") timeLimit = timeLimit-1 timeLeft.text = timeLimit if(timeLimit==0)then timer.pause( event.source ) currentscore = currentscore + tapCount composer.removeScene("scene2") composer.gotoScene( "scene2", { effect = "fade", time = 800, params = { level="Level 1", score=currentscore } } ) end end

That should get you past the problem you’re currently experiencing. 

thanks! it is done. everything is working…

my question is about timer.i have 2 scenes(scene2.lua,scene3.lua). scene2 has timer that is working good when first appearance.After time ellapsed i am showing scene3 to show score.Scene3 has pkay agian button to play game agian by forwarding to scene2.Timer doesn’t work again. My codes below (scene2.lua)

Are you removing your scene2 before you go to it from scene3 by calling:

composer.removeScene("scene2")

???

Rob

Yes timer is worked. By using “composer.removeScene(“scene2”)” BUT this time, my physics not nowking properly and second time play again not working.

By the way; if you remove scene2, you can not ga again by pushing “playagain”. My entirecode is below :

(PLEASE RUN MY CODE.While deciding to write here.PLEASE

scene2->scene1.lua

scene3->scene2.lua

scene2.lua:

local composer = require( "composer" ) local scene = composer.newScene() local tapCount = 0 local timeLimit = 5 local currentscore =0 -- Later... local sceneGroup local timerDown local function timerDown( event ) print("timer çalışıyor") timeLimit = timeLimit-1 timeLeft.text = timeLimit if(timeLimit==0)then timer.pause( event.source ) currentscore = currentscore + tapCount composer.gotoScene( "scene2", { effect = "fade", time = 800, params = { level="Level 1", score=currentscore } } ) end end local background = display.newImageRect( "background.png", 360, 570 ) background.x = display.contentCenterX background.y = display.contentCenterY local platform = display.newImageRect( "platform.png", 300, 50 ) platform.x = display.contentCenterX platform.y = display.contentHeight-25 platform.myName="platform" local balloon = display.newImageRect( "balloon.png", 112, 112 ) balloon.x = display.contentCenterX balloon.y = display.contentCenterY balloon.myName="balloon" balloon.alpha = 0.8 local tapText = display.newText( tapCount, display.contentCenterX, 20, native.systemFont, 40 ) tapText:setFillColor( 0, 0, 0 ) timeLeft = display.newText("Time", display.contentCenterX+100,display.contentCenterY-250,native.systemFont, 20) timeLeft:setTextColor(255,0,0) local physics = require( "physics" ) local function pushBalloon() balloon:applyLinearImpulse( 0, -0.75, balloon.x, balloon.y ) tapCount = tapCount + 1 tapText.text = tapCount end local function onCollision( event ) if(event.object2.myName == "balloon" and event.object1.myName == "platform") then if(tapCount\>0) then tapCount = tapCount -1 tapText.text = tapCount end end end balloon:addEventListener( "tap", pushBalloon ) Runtime:addEventListener( "collision", onCollision ) function scene:create( event ) sceneGroup = self.view print("working create") end function scene:show( event ) timerDown = timer.performWithDelay(1000,timerDown,timeLimit) local sceneGroup = self.view local phase = event.phase print("working show") if ( phase == "will" ) then sceneGroup:insert(background) sceneGroup:insert(platform) sceneGroup:insert(balloon) sceneGroup:insert(tapText) sceneGroup:insert(timeLeft) physics.start() physics.addBody( platform, "static" ) physics.addBody( balloon, "dynamic", { radius=50, bounce=0.3 } ) elseif ( phase == "did" ) then end end function scene:hide( event ) end function scene:destroy( event ) end scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) return scene

scene2.lua:

local composer = require( "composer" ) local scene = composer.newScene() local sceneGroup local background = display.newImageRect( "background.png", 360, 570 ) background.x = display.contentCenterX background.y = display.contentCenterY local widget = require("widget") local bottomTabButtons ={ { width=50, height=32, defaultFile="play.png", overFile="playhover.png", onPress=function() composer.gotoScene( "scene1" ) end}, { width=50, height=32, defaultFile="quit.png", overFile="quithover.png", onPress=function() os.exit() end}, } local bottomBar = widget.newTabBar{ top = display.contentHeight-40, buttons = bottomTabButtons } function scene:create( event ) sceneGroup = self.view local params = event.params local myText1 = display.newText( params.score, display.contentCenterX, display.contentCenterY+50, native.systemFont, 48 ) local myText2 = display.newText( "Your Score !", display.contentCenterX, display.contentCenterY, native.systemFont, 48 ) myText1:setFillColor( 1, 0, 0 ) myText2:setFillColor( 1, 0, 0 ) sceneGroup:insert(background) sceneGroup:insert(myText1) sceneGroup:insert(myText2) sceneGroup:insert(bottomBar) end function scene:show( event ) if ( phase == "will" ) then composer.removeScene("scene1") elseif ( phase == "did" ) then end end function scene:hide( event ) end function scene:destroy( event ) end scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) return scene

We can’t run your code. We don’t have the complete project with all of the images… But what would be more helpful is to post any errors you’re getting in your console log (or warnings).

Also, when you ask the community to run your project for you and fix it, you’re asking them to do the work for you and you really won’t learn from it. Programming is challenging. Debugging can be frustrating. But when you do learn and figure it out, it’s such a great feeling.

Now back to helping  you. The code above doesn’t seem to be right. You posted two “scene2.lua” files. The one I think  you expect to be scene3.lua, I don’t see where you are removing scene2.

On a side note, you should not use os.exit(). You should instead use native.requestExit(). iOS apps should never exit like this and Apple will reject them. On Android some users expect apps to exit, but it’s generally not a needed feature. But you should use native.requestExit() instead.

Rob

Rob;

i don’t want any body to improve my project . But you are not looking for RemoveScene method also you are not runnig my code before writing your idea. how do you understand my problem. if you look at my code you can see easly my RemoveScene above.i have 12 years experience as C# developer. i know how to write program and i know any aspect of programmnig (Challenging debugging bla bla bla …) Please don’t waste my time off by writing weird ( not helping) comments . are they useful ? ( i guess “no” because not only me lots of people will have the same headache of it) i know something a button should work always. clicking once “great” secand click it  is messed up. Please be helpful.   

That’s a great way to get people to help you :smiley:

Rob might be a Corona staff member but a certain level of politeness and appreciation would go a long way, instead of accusing him of wasting your time.