How to destroy/forget about a scene when you go to a new one.

Hey guys I can’t seem to figure out how to make it so when you “lose” the game you will go to the next scene and it will remove the “game” scene. I have it so it switches scenes when you lose but it still is calling one of the print functions in the level1.lua file once you have gone to the lose scene. I did get it so it removes it and when I go back to the main menu screen it goes back to the lose.lua scene because I lost but it did not remove everything in the scene. Is there a way to “reset” the scene upon re-entry.

--level1 local composer = require( "composer" ) local scene = composer.newScene() -- ----------------------------------------------------------------------------------------------------------------- -- All code outside of the listener functions will only be executed ONCE unless "composer.removeScene()" is called. -- ----------------------------------------------------------------------------------------------------------------- -- local forward references should go here -- ------------------------------------------------------------------------------- -- "scene:create()" function scene:create( event ) local sceneGroup = self.view local zombiegroup = display.newGroup() local physics = require ("physics"); physics.start(); physics.setGravity(0,0) local ground = display.newImage("bg.png",240,150,600,600) ground:scale(0.57,0.68) ground:rotate(-90) local cannon1 = display.newImage("cannon.png",math.random(20,90),50) cannon1:scale(0.15,0.15) local cannon2 = display.newImage("cannon.png",math.random(10,300),270,30,30) cannon2:scale(0.15,0.15) local money = 0 local bullets1 = 10 local bullets2 = 10 local numberSmiles = 1 --local variable; amount can be changed local moneydis = display.newText(money,100,70,native.systemFont,24) sceneGroup:insert(moneydis) local boat = display.newRect(0,99,30,400) physics.addBody(boat,"static") local function die(event) if (event.phase == "began") then print("you lose") composer.gotoScene("lose") end end boat:addEventListener("collision",die) local function spawnSmiles() for i=1,numberSmiles do local zombie1 = display.newImage("zombie.png",430,50, 45, 45); -- smile:setReferencePoint(display.CenterReferencePoint); --not necessary; center is default physics.addBody(zombie1,"dynamic") transition.to( zombie1, { time=math.random(2000,8000), x=-10, y=50} ); -- Note: any time less than one frame period is the same as waiting one frame if 0\<400 then if bullets1 \>= 1 then print("shoot") local bullet1 = display.newRect(cannon1.x,cannon1.y,30,10) transition.to(bullet1, { time=9000, x=zombie1.x, y=zombie1.y} ); sceneGroup:insert(bullet1) physics.addBody(bullet1,"static") bullets1 = bullets1-1 moneydis.text = money if bullets1\<=2 then local reloadtxt= display.newText("Reload Cannon 1",300,50,native.systemFont,25) reloadtxt:setFillColor(0,0,0) sceneGroup:insert(reloadtxt) local function reload1( self,event ) if money\>10 then bullets1= bullets1+10 money = money-10 reloadtxt:removeSelf() end end reloadtxt:addEventListener("touch",reload1) end local function bulletcollision(self, event ) display.remove(bullet1) display.remove(zombie1) money=money+math.random(0,2) end bullet1:addEventListener("collision",bulletcollision) end end end end timer.performWithDelay( 700, spawnSmiles, 0 ) --fire every 10 seconds local function spawnSmiles1() for i=1,numberSmiles do local zombie2 = display.newImage("zombie.png",430,270, 45, 45); physics.addBody(zombie2,"dynamic") -- smile:setReferencePoint(display.CenterReferencePoint); --not necessary; center is default transition.to( zombie2, { time=math.random(6000,8000), x=-10, y=270} ); if 0\<400 then if bullets2\>=1 then print("shoot") if bullets2\<=0 then local reloadtxt= display.newText("Reload Cannon 2",300,150,native.systemFont,25) end local bullet2 = display.newRect(10,10,30,10) transition.to(bullet2, { time=9000, x=zombie2.x, y=zombie2.y} ); physics.addBody(bullet2,"static") local function collision2( event ) bullet2:removeSelf() zombie2:removeSelf() money=money+2 bullets2=bullets2-1 end bullet2:addEventListener("collision",collision2) end end end end timer.performWithDelay( 700, spawnSmiles1, 0 ) --fire every 10 seconds local function spawnSmiles3() for i=1,numberSmiles do local zombie3 = display.newImage("zombie.png",430,150, 45, 45); physics.addBody(zombie3,"dynamic") transition.to( zombie3, { time=math.random(6000,8000), x=-10, y=150} ); local function shoothuman( event ) if money\>=5 then zombie3:removeSelf() money = money- 5 money=money+3 moneydis.text = money end end zombie3:addEventListener("touch",shoothuman) end end timer.performWithDelay( 1000, spawnSmiles3, 0 ) --fire every 10 seconds sceneGroup:insert(ground) sceneGroup:insert(cannon1) sceneGroup:insert(cannon2) sceneGroup:insert(boat) end -- "scene:show()" function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Called when the scene is still off screen (but is about to come on screen). elseif ( phase == "did" ) then -- Called when the scene is now on screen. -- Insert code here to make the scene come alive. -- Example: start timers, begin animation, play audio, etc. end end -- "scene:hide()" function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Called when the scene is on screen (but is about to go off screen). -- Insert code here to "pause" the scene. -- Example: stop timers, stop animation, stop audio, etc. elseif ( phase == "did" ) then -- Called immediately after scene goes off screen. end end -- "scene:destroy()" function scene:destroy( event ) local sceneGroup = self.view -- Called prior to the removal of scene's view ("sceneGroup"). -- Insert code here to clean up the scene. -- Example: remove display objects, save state, etc. end -- ------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ------------------------------------------------------------------------------- return scene

--lose.lua local composer = require( "composer" ) local scene = composer.newScene() -- ----------------------------------------------------------------------------------------------------------------- -- All code outside of the listener functions will only be executed ONCE unless "composer.removeScene()" is called. -- ----------------------------------------------------------------------------------------------------------------- -- local forward references should go here -- ------------------------------------------------------------------------------- -- "scene:create()" function scene:create( event ) local sceneGroup = self.view local txt = display.newText("you lose",230,100,native.systemFont,40) local reloadbtn = display.newText("Main Menu",230,200,native.systemFont,30) reloadbtn:setFillColor(1,0,0) local function changescene( event ) composer.gotoScene("mainmenu") end -- Initialize the scene here. sceneGroup:insert(txt) sceneGroup:insert(reloadbtn) reloadbtn:addEventListener("touch",changescene) -- Example: add display objects to "sceneGroup", add touch listeners, etc. end -- "scene:show()" function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then elseif ( phase == "did" ) then composer.removeScene("level1") end end -- "scene:hide()" function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Called when the scene is on screen (but is about to go off screen). -- Insert code here to "pause" the scene. -- Example: stop timers, stop animation, stop audio, etc. elseif ( phase == "did" ) then -- Called immediately after scene goes off screen. end end -- "scene:destroy()" function scene:destroy( event ) local sceneGroup = self.view -- Called prior to the removal of scene's view ("sceneGroup"). -- Insert code here to clean up the scene. -- Example: remove display objects, save state, etc. end -- ------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ------------------------------------------------------------------------------- return scene

Why do you need to remove/forget it when you leave it?

In an ideal world, you wouldn’t remove them if you’re going back to them.  It takes a little programming if you want to “reset” the scene.  It’s a matter of positioning everything in scene:show()'s “will” phase.  Then starting any physics, animations in the scene:show()'s “did” phase.  When you leave the scene, make sure everything you started, stops in the scene:hide()'s “will” phase.

In the “I want to do this with minimal effort” mode and it’s a game, you would still start/stop things as above so that no Runtime listeners, transitions, physics are running.  Then leave the game scene in memory and when you are ready to return to it, do a:

composer.removeScene(“game”) – or whatever you named it.    If the scene you’re arriving in can only be gotten to from the one you want to dump, then you can in the scene:show()'s “did” phase, call composer.removeScene(“game”)

Rob

Why do you need to remove/forget it when you leave it?

In an ideal world, you wouldn’t remove them if you’re going back to them.  It takes a little programming if you want to “reset” the scene.  It’s a matter of positioning everything in scene:show()'s “will” phase.  Then starting any physics, animations in the scene:show()'s “did” phase.  When you leave the scene, make sure everything you started, stops in the scene:hide()'s “will” phase.

In the “I want to do this with minimal effort” mode and it’s a game, you would still start/stop things as above so that no Runtime listeners, transitions, physics are running.  Then leave the game scene in memory and when you are ready to return to it, do a:

composer.removeScene(“game”) – or whatever you named it.    If the scene you’re arriving in can only be gotten to from the one you want to dump, then you can in the scene:show()'s “did” phase, call composer.removeScene(“game”)

Rob