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