scene is still in background after composer.removeScene()

Hey guys I am having trouble with composer. So when you lose in the game it changes scenes but it works but the physic bodies and stuff are still in the background when i set the draw mode to hybrid. Any thoughts?

--game1.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 BG = display.newImage ("BG.png") sceneGroup:insert(BG) 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 local group = display.newGroup() local smile local physics = require("physics") physics.start() local Gem = display.newImage("Gem.png", 50, 50) Gem.x = display.contentCenterX Gem.y = display.contentCenterY physics.addBody(Gem) Gem.isSensor = false local numberSmiles = 1 --local variable; amount can be changed sceneGroup:insert(Gem) physics.setGravity(0,0) physics.setDrawMode( "hybrid" ) local function spawnTop() for i=1,numberSmiles do local smile = display.newImage("Spear.png", math.random(-10, 400),-200 ); --smile:scale(0.02,0.02) physics.addBody( smile,"static", { density=0, friction=0, bounce=0 } ) smile.x = display.contentCenterX smile:rotate(180) -- smile:setReferencePoint(display.CenterReferencePoint); --not necessary; center is default smile.isFixedRotation = true transition.to( smile, { time=3000 , x = display.contentCenterX , y = display.contentCenterY} ); local function removeSmiles( self,event ) smile:removeSelf() end smile:addEventListener("tap",removeSmiles) sceneGroup:insert(smile) end end local timer1 = timer.performWithDelay( math.random(1000,2000), spawnTop, -1 ) --fire every 10 seconds local numberSmiles = 1 --local variable; amount can be changed -- local function spawnBottom() for i=1,numberSmiles do local smile = display.newImage("Spear.png", math.random(-10, 400),700 ); --smile.x = display.contentCenterX smile:scale(0.02,0.02) physics.addBody( smile, "static",{ density=0, friction=0, bounce=0 } ) -- smile:setReferencePoint(display.CenterReferencePoint); --not necessary; center is default smile.isFixedRotation = true transition.to( smile, { time=3000 , x = display.contentCenterX , y = display.contentCenterY} ); local function removeSmiles( self,event ) smile:removeSelf() end smile:addEventListener("tap",removeSmiles) sceneGroup:insert(smile) end end local timer1 = timer.performWithDelay( math.random(2000,3000), spawnBottom, -1 ) --fire every 10 seconds local numberSmiles = 1 --local variable; amount can be changed local function spawnLeft() for i=1,numberSmiles do local smile = display.newImage("Spear.png", -50,math.random(0,500), 500 ); smile.y = display.contentCenterY --smile:scale(0.02,0.02) smile:rotate(90) -- smile:setReferencePoint(display.CenterReferencePoint); --not necessary; center is default physics.addBody( smile, "static",{ density=0, friction=0, bounce=0 } ) smile.isFixedRotation = true transition.to( smile, { time=1500 , x = display.contentCenterX , y = display.contentCenterY} ); local function removeSmiles( self,event ) smile:removeSelf() end smile:addEventListener("tap",removeSmiles) sceneGroup:insert(smile) end end local timer1 = timer.performWithDelay( math.random(1500,2500), spawnLeft, -1 ) --fire every 10 seconds local numberSmiles = 1 --local variable; amount can be changed local function spawnRight() for i=1,numberSmiles do local smile = display.newImage("Spear.png", 500,math.random(0,400) ); smile.y = display. contentCenterY --smile:scale(0.02,0.02) smile:rotate(-90) --smile:rotate(gembehind.radius) -- smile:setReferencePoint(); --not necessary; center is default physics.addBody( smile, "static",{ density=0, friction=0, bounce=0 } ) smile.isFixedRotation = true transition.to( smile, { time=2000 , x = display.contentCenterX , y = display.contentCenterY} ); local function removeSmiles( self,event ) smile:removeSelf() end smile:addEventListener("tap",removeSmiles) sceneGroup:insert(smile) end end local timer1 = timer.performWithDelay( math.random(1700,2700), spawnRight, -1 ) --fire every 10 seconds local function spawnTopRight() for i=1,numberSmiles do local smile = display.newImage("Spear.png", 328,-60); --smile:scale(0.02,0.02) smile:rotate(213) --smile:rotate(gembehind.radius) -- smile:setReferencePoint(); --not necessary; center is default physics.addBody( smile, "static",{ density=0, friction=0, bounce=0 } ) smile.isFixedRotation = true transition.to( smile, { time=2000 , x = display.contentCenterX , y = display.contentCenterY} ); local function removeSmiles( self,event ) smile:removeSelf() end smile:addEventListener("tap",removeSmiles) sceneGroup:insert(smile) end end local timer1 = timer.performWithDelay( math.random(1500,2500), spawnTopRight, -1 ) --fire every 10 seconds local function spawnBottomRight() for i=1,numberSmiles do local smile = display.newImage("Spear.png", 335,550 ); --smile:scale(0.02,0.02) smile:rotate(330) --smile:rotate(gembehind.radius) -- smile:setReferencePoint(); --not necessary; center is default physics.addBody( smile,"static", { density=0, friction=0, bounce=0 } ) smile.isFixedRotation = true transition.to( smile, { time=2000 , x = display.contentCenterX , y = display.contentCenterY} ); local function removeSmiles( self,event ) smile:removeSelf() end smile:addEventListener("tap",removeSmiles) sceneGroup:insert(smile) end end local timer1 = timer.performWithDelay( math.random(1700,2700), spawnBottomRight, -1 ) --fire every 10 seconds local function spawnBottomLeft() for i=1,numberSmiles do local smile = display.newImage("Spear.png", -20,560 ); --smile:scale(0.02,0.02) smile:rotate(32) --smile:rotate(gembehind.radius) -- smile:setReferencePoint(); --not necessary; center is default physics.addBody( smile,"static", { density=0, friction=0, bounce=0 } ) smile.isFixedRotation = true transition.to( smile, { time=2000 , x = display.contentCenterX , y = display.contentCenterY} ); local function removeSmiles( self,event ) smile:removeSelf() end smile:addEventListener("tap",removeSmiles) sceneGroup:insert(smile) end end local timer1 = timer.performWithDelay( math.random(1500,2500), spawnBottomLeft, -1 ) --fire every 10 seconds local function spawnTopLeft() for i=1,numberSmiles do local smile = display.newImage("Spear.png", -40,-120 ); --smile:scale(0.02,0.02) smile:rotate(150) --smile:rotate(gembehind.radius) -- smile:setReferencePoint(); --not necessary; center is default physics.addBody( smile, "staic",{ density=0, friction=0, bounce=0 } ) smile.isFixedRotation = true transition.to( smile, { time=2000 , x = display.contentCenterX , y = display.contentCenterY} ); local function removeSmiles( self,event ) smile:removeSelf() end smile:addEventListener("tap",removeSmiles) sceneGroup:insert(smile) local function onCollision(self,event ) print("changing scenes!") composer.removeScene("Game1") composer.gotoScene("GameOver") timer.cancel(timer1) end Gem:addEventListener("collision",onCollision) end end local timer1 = timer.performWithDelay( math.random(1700,2700), spawnTopLeft, -1 ) --fire every 10 seconds physics.addBody(Gem,"dynamic") 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

Hi @tpjacobson01,

Although it may seem odd, this is expected. By default, Composer actually keeps scenes in memory when you change to another scene, on the assumption that you may return to that scene reasonably soon. This means that the physics objects still exist, but because of the way physics objects work with display groups (remember that a Composer scene is effectively a display group), those objects will visually move off screen or disappear, but the physical objects (as you see in “hybrid” mode) will still exist.

Of course, there are ways to handle this if you don’t like that behavior, including:

– When you leave the scene, manually destroy all physics objects.

– When you leave the scene, explicitly remove it from memory using “composer.removeScene()”.

– Set Composer to “recycle” scenes when they are exited, meaning that the scene’s “view” will be destroyed, and the physics objects along with it.

Hope this helps,

Brent

Hi @tpjacobson01,

Although it may seem odd, this is expected. By default, Composer actually keeps scenes in memory when you change to another scene, on the assumption that you may return to that scene reasonably soon. This means that the physics objects still exist, but because of the way physics objects work with display groups (remember that a Composer scene is effectively a display group), those objects will visually move off screen or disappear, but the physical objects (as you see in “hybrid” mode) will still exist.

Of course, there are ways to handle this if you don’t like that behavior, including:

– When you leave the scene, manually destroy all physics objects.

– When you leave the scene, explicitly remove it from memory using “composer.removeScene()”.

– Set Composer to “recycle” scenes when they are exited, meaning that the scene’s “view” will be destroyed, and the physics objects along with it.

Hope this helps,

Brent