Previous page problem

Hi; my question is simple but i can not solve it. i have 3 steps app. main,scene1,scene2 . i am showing my score on scene2 if i decide to play again i wanna user to press “PLAY” button. but it can not go back scene1. on the other hand scenegroup is not working. Everything is disseapering. i attached my app files below.

Main .lua:

 local composer = require "composer" display.setStatusBar( display.HiddenStatusBar ) local background = display.newImageRect( "background.png", 360, 570 ) background.x = display.contentCenterX background.y = display.contentCenterY local widget = require( "widget" ) -- Function to handle button events local function handleButtonEvent( event ) if ( "ended" == event.phase ) then composer.gotoScene( "scene1" ) end end -- Create the widget local buttonplay = widget.newButton( { label = "button", onEvent = handleButtonEvent, emboss = false, -- Properties for a rounded rectangle button shape = "roundedRect", width = 200, height = 40, cornerRadius = 2, fillColor = { default={1,0,0,1}, over={1,0.1,0.7,0.4} }, strokeColor = { default={1,0.4,0,1}, over={0.8,0.8,1,1} }, strokeWidth = 4 } ) buttonplay.x = display.contentCenterX buttonplay.y = display.contentCenterY -- Change the button's label text buttonplay:setLabel( "Play" )

scene1.lua

local composer = require( "composer" ) local scene = composer.newScene() local tapCount = 0 local timeLimit = 3 local currentscore =0 -- Later... local function timerDown() timeLimit = timeLimit-1 timeLeft.text = timeLimit if(timeLimit==0)then currentscore = currentscore + tapCount composer.gotoScene( "scene2", { effect = "fade", time = 800, params = { level="Level 1", score=currentscore } } ) end end timer.performWithDelay(1000,timerDown,timeLimit) 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" ) physics.start() physics.addBody( platform, "static" ) physics.addBody( balloon, "dynamic", { radius=50, bounce=0.3 } ) local function pushBalloon() balloon:applyLinearImpulse( 0, -0.75, balloon.x, balloon.y ) tapCount = tapCount + 1 tapText.text = tapCount print("%s","%d", "currenscore: ", 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 print("%s","%d", "currenscore: ", tapCount) end end end balloon:addEventListener( "tap", pushBalloon ) Runtime:addEventListener( "collision", onCollision ) -- create() function scene:create( event ) local sceneGroup = self.view -- Code here runs when the scene is first created but has not yet appeared on screen print("slm sanırım başardım.") end -- show() function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Code here runs when the scene is still off screen (but is about to come on screen) elseif ( phase == "did" ) then -- Code here runs when the scene is entirely on screen end end -- hide() function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Code here runs when the scene is on screen (but is about to go off screen) elseif ( phase == "did" ) then -- Code here runs immediately after the scene goes entirely off screen end end -- destroy() function scene:destroy( event ) local sceneGroup = self.view -- Code here runs prior to the removal of scene's view end -- ----------------------------------------------------------------------------------- -- Scene event function listeners -- ----------------------------------------------------------------------------------- scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ----------------------------------------------------------------------------------- return scene

scene2.lua ( PLAY AGAIN IS NOT WORKING.) Actually i can not use sceneGroup

local composer = require( "composer" ) local scene = composer.newScene() -- show() function scene:show( event ) local sceneGroup = self.view local params = event.params local background = display.newImageRect( "background.png", 360, 570 ) background.x = display.contentCenterX background.y = display.contentCenterY local myText = display.newText( params.score, 100, 200, native.systemFont, 16 ) myText:setFillColor( 1, 0, 0 ) --sceneGroup:insert(myText) local widget = require("widget") local bottomTabButtons ={ { width=32, height=32, defaultFile="play.png", overFile="playhover.png", onPress=function() composer.gotoScene( "scene1" ) end}, { width=32, height=32, defaultFile="quit.png", overFile="quithover.png", onPress=function() composer.gotoScene( "scene1" ) end}, } local bottomBar = widget.newTabBar{ top = display.contentHeight-40, buttons = bottomTabButtons } --sceneGroup:insert(background) --sceneGroup:insert(myText) --sceneGroup:insert(bottomBar) end -- hide() function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Code here runs when the scene is on screen (but is about to go off screen) elseif ( phase == "did" ) then -- Code here runs immediately after the scene goes entirely off screen end end -- destroy() function scene:destroy( event ) local sceneGroup = self.view -- Code here runs prior to the removal of scene's view end -- ----------------------------------------------------------------------------------- -- Scene event function listeners -- ----------------------------------------------------------------------------------- scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ----------------------------------------------------------------------------------- return scene

Here are a couple of key points to take away from your question.

Valid Composer scenes have a display.newGroup() that’s part of the scene object. If you name  your scene object “scene” like our templates do, then once you do:

local scene = composer.newScene()

every where else in that module, you can now access its group using:

scene.view

i.e.

scene.view:insert( background )

Because it’s technically (and not recommended) possible to have multiple scenes in one file (and please don’t do this), the proper object oriented way is to access the “self” variable on scene methods. This is why:

function scene:create( event )

(note the : operator adding create to the scene object) has a line in it:

    local sceneGroup = self.view

“self” is “scene” in the case of a file with a single scene in it. Therefore:

sceneGroup = self.view = scene.view

You only have access to self.view if you’re functions have scene: prefixed to them and you’ve added those functions to the scene object.  “sceneGroup” seems like a nice way to name things.  To make your life easier, if you create a local function outside of one of the scene: functions, you can do:

local function myFunction()      local sceneGroup = scene.view  -- not self.view, you don't have a "self" here. But self = scene, so you can use scene.      -- rest of function end

Point 2. Only objects expressly put into the scene’s view group is managed by composer. That means if you don’t do this, when you change scenes those images will stay on the screen and in front of your scene. You have this issue in main.lua when creating the button, which leads to point 3.

Point 3. Main is never a scene. You initialize stuff. If you create anything that shows on the screen you have to manually remove it before going to the first scene unless you want it permanently there. So initialize things (ads, in-app purchases, etc.) go to your first scene and then start displaying things. You’re creating the “play” button here. I would defer his to a specific menu scene and not have it as part of main.lua.

Back to point 2…

In your scene1.lua, you create a bunch of display objects in the module’s main chunk. You never add them to scene.view/self.view/sceneGroup. Therefore they become unmanged objects on top of any scene’s managed objects. You should move this code inside of the scene:create() function and make sure to insert them into “sceneGroup”.

As for scene2, you have least have the object creation in a composer scene event, but most of that should be in scene:create(). I’d like to know what errors your getting. But at a minimum we need to talk about point 4.

Point 4. scene:show() and scene:hide() are executed TWICE on ever scene entrance and exit. The scene:show() function is called once when the scene is ready to transition on screen. It’s called a second time after it’s successfully on screen. If you don’t want things happening twice (like creating the same object twice on top of each other… another reason to do this in scene:create()) then you have to use an if - then statement which you can see in scene:hide(). The first call for scene show event.phase will equal “will”. The second call, event.phase will equal “did”. You can use this information to start up things like timers, transitions, Collision event handling and so on.

Hope this  helps.

Rob

Hi Thanks your help but. i fixed this problem. But now; my timer is not working properly. i can go to scene(scene3.lua) but if i go back scene2.lua. My timer is not start again. i used timer.Resume() doest work. 

My code is 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

Here are a couple of key points to take away from your question.

Valid Composer scenes have a display.newGroup() that’s part of the scene object. If you name  your scene object “scene” like our templates do, then once you do:

local scene = composer.newScene()

every where else in that module, you can now access its group using:

scene.view

i.e.

scene.view:insert( background )

Because it’s technically (and not recommended) possible to have multiple scenes in one file (and please don’t do this), the proper object oriented way is to access the “self” variable on scene methods. This is why:

function scene:create( event )

(note the : operator adding create to the scene object) has a line in it:

    local sceneGroup = self.view

“self” is “scene” in the case of a file with a single scene in it. Therefore:

sceneGroup = self.view = scene.view

You only have access to self.view if you’re functions have scene: prefixed to them and you’ve added those functions to the scene object.  “sceneGroup” seems like a nice way to name things.  To make your life easier, if you create a local function outside of one of the scene: functions, you can do:

local function myFunction()      local sceneGroup = scene.view  -- not self.view, you don't have a "self" here. But self = scene, so you can use scene.      -- rest of function end

Point 2. Only objects expressly put into the scene’s view group is managed by composer. That means if you don’t do this, when you change scenes those images will stay on the screen and in front of your scene. You have this issue in main.lua when creating the button, which leads to point 3.

Point 3. Main is never a scene. You initialize stuff. If you create anything that shows on the screen you have to manually remove it before going to the first scene unless you want it permanently there. So initialize things (ads, in-app purchases, etc.) go to your first scene and then start displaying things. You’re creating the “play” button here. I would defer his to a specific menu scene and not have it as part of main.lua.

Back to point 2…

In your scene1.lua, you create a bunch of display objects in the module’s main chunk. You never add them to scene.view/self.view/sceneGroup. Therefore they become unmanged objects on top of any scene’s managed objects. You should move this code inside of the scene:create() function and make sure to insert them into “sceneGroup”.

As for scene2, you have least have the object creation in a composer scene event, but most of that should be in scene:create(). I’d like to know what errors your getting. But at a minimum we need to talk about point 4.

Point 4. scene:show() and scene:hide() are executed TWICE on ever scene entrance and exit. The scene:show() function is called once when the scene is ready to transition on screen. It’s called a second time after it’s successfully on screen. If you don’t want things happening twice (like creating the same object twice on top of each other… another reason to do this in scene:create()) then you have to use an if - then statement which you can see in scene:hide(). The first call for scene show event.phase will equal “will”. The second call, event.phase will equal “did”. You can use this information to start up things like timers, transitions, Collision event handling and so on.

Hope this  helps.

Rob

Hi Thanks your help but. i fixed this problem. But now; my timer is not working properly. i can go to scene(scene3.lua) but if i go back scene2.lua. My timer is not start again. i used timer.Resume() doest work. 

My code is 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