How to cleanup the scene?

Hello, I encountered with an error when I tried to clean up the scene. Below I have pasted the code. This code stops physics engine and doesn’t let create new objects but it does not destroy previously created objects which I want. I have studied  Star explorer  project.  

I have added in menu.lua file as it says in the project and did other things in Level1 file.

local function gotoGame()

composer.removeScene( “level1” )

composer.gotoScene( “level1”, { time=800, effect=“crossFade” } )

end

Here is the code for LEVEL1


– level1.lua


local composer = require( “composer” )

local scene = composer.newScene()

– include Corona’s “physics” library

local physics = require “physics”


local physics = require( “physics” )

physics.start()

physics.setGravity(0,0.5)

local function speed()

physics.setGravity(0,9.8)

end

local myClosure = function() return speed()

end

timer.performWithDelay( 10000, myClosure, 1)

function scene:create( event )

local platform = display.newRect( 0, 0, 60, 30 )

platform:setFillColor( 0.2, 0.2, 0.3 )

platform.surfaceType = “magic”

platform.x, platform.y = display.contentCenterX, display.contentCenterY+80

physics.addBody( platform, “static”, { bounce=0.0, friction=0.3 } )

local platform2 = display.newRect( 0, 0, 60, 30 )

platform2:setFillColor( 0.9, 0.1, 0.3 )

platform2.surfaceType = “magicm”

platform2.x, platform2.y = display.contentCenterX, display.contentCenterY+200

physics.addBody( platform2, “static”, { bounce=0.0, friction=0.3 } )

local function box()

local ball = display.newCircle( 0.1, 0.6, 30)

ball:setFillColor( 0.2, 0.2, 0.3 )

local ball2 = display.newCircle( 0, 0, 30 )

ball2:setFillColor( 0.9, 0.1, 0.3 )

rand = math.random(10)

if (rand < 5) then 

ball.x, ball.y = display.contentCenterX+30, display.contentCenterY-200

physics.addBody( ball, “dynamic”, { bounce=0.0, radius=20 } )

ball2.x, ball2.y = display.contentCenterX+100, display.contentCenterY-150

physics.addBody( ball2, “dynamic”, { bounce=0.0, radius=20 } )

elseif ( rand < 9 ) then

ball.x, ball.y = display.contentCenterX+150, display.contentCenterY-150

physics.addBody( ball, “dynamic”, { bounce=0.0, radius=20 } )

ball2.x, ball2.y = display.contentCenterX-50, display.contentCenterY-150

physics.addBody( ball2, “dynamic”, { bounce=0.0, radius=20 } )

else

ball.x, ball.y = display.contentCenterX+100, display.contentCenterY-150

physics.addBody( ball, “dynamic”, { bounce=0.0, radius=20 } )

ball2.x, ball2.y = display.contentCenterX+60, display.contentCenterY-20

physics.addBody( ball2, “dynamic”, { bounce=0.0, radius=20 } )

end

local function dragplatfrom( event )

    local platform = event.target

    local phase = event.phase

    if ( “began” == phase ) then

        – Set touch focus on the ball2

        display.currentStage:setFocus( platform )

        – Store initial offset position

        platform.touchOffsetX = event.x - platform.x

         

    elseif ( “moved” == phase ) then

        – Move the ball2 to the new touch position

        platform.x = event.x - platform.touchOffsetX

        

    elseif ( “ended” == phase or “cancelled” == phase ) then

        – Release touch focus on the ball2

        display.currentStage:setFocus( nil )

    end

    return true  – Prevents touch propagation to underlying objects

end

platform:addEventListener( “touch”, dragplatfrom )

local function dragplatfrom2( event )

    local platform2 = event.target

    local phase = event.phase

    if ( “began” == phase ) then

        – Set touch focus on the ball2

        display.currentStage:setFocus( platform2 )

        – Store initial offset position

        platform2.touchOffsetX = event.x - platform2.x

         

    elseif ( “moved” == phase ) then

        – Move the ball2 to the new touch position

        platform2.x = event.x - platform2.touchOffsetX

        

    elseif ( “ended” == phase or “cancelled” == phase ) then

        – Release touch focus on the ball2

        display.currentStage:setFocus( nil )

    end

    return true  – Prevents touch propagation to underlying objects

end

platform2:addEventListener( “touch”, dragplatfrom2 )

local function endGame()

composer.gotoScene( “menu”, { time=800, effect=“crossFade” } )

end

local function onCollision( self, event )

   local collideObject = event.other

   if ( collideObject.surfaceType == “magic” ) then

       display.remove(ball)

timer.performWithDelay( 200, endGame )

 end

end

ball.collision = onCollision

ball:addEventListener(“collision”)

local function onCollision( self, event )

   local collideObject = event.other

   if ( collideObject.surfaceType == “magicm” ) then

       display.remove(ball2)

timer.performWithDelay( 200, endGame )

 end

end

ball2.collision = onCollision

ball2:addEventListener(“collision”)

end

local function gameLoop()

box()

 end

 gameLoopTimer = timer.performWithDelay( 3000, gameLoop, 0 )

end

function scene:show( event )

local sceneGroup = self.view

local phase = event.phase

if phase == “will” then

– Called when the scene is still off screen and is about to move on screen

elseif phase == “did” then

– Called when the scene is now on screen

– 

– INSERT code here to make the scene come alive

– e.g. start timers, begin animation, play audio, etc.

physics.start()

end

end

function scene:hide( event )

local sceneGroup = self.view

local phase = event.phase

if event.phase == “will” then

timer.cancel( gameLoopTimer )

Runtime:removeEventListener( “collision”, onCollision )

physics.pause()

elseif phase == “did” then

– Called when the scene is now off screen

end

end

function scene:destroy( event )

– Called prior to the removal of scene’s “view” (sceneGroup)

– 

– INSERT code here to cleanup the scene

– e.g. remove display objects, remove touch listeners, save state, etc.

local sceneGroup = self.view

package.loaded[physics] = nil

physics = nil

end


– Listener setup

scene:addEventListener( “create”, scene )

scene:addEventListener( “show”, scene )

scene:addEventListener( “hide”, scene )

scene:addEventListener( “destroy”, scene )


return scene

Here is the code for Menu


– menu.lua


local composer = require( “composer” )

local scene = composer.newScene()

– include Corona’s “widget” library

local widget = require “widget”


local function gotoGame()

composer.removeScene( “level1” )

    composer.gotoScene( “level1”, { time=800, effect=“crossFade” } )

end

function scene:create( event )

local sceneGroup = self.view

– Code here runs when the scene is first created but has not yet appeared on screen

local background = display.newImageRect( sceneGroup, “6.png”, 800, 1400 )

background.x = display.contentCenterX

background.y = display.contentCenterY

local playButton = display.newText( sceneGroup, “Play”, display.contentCenterX, display.contentCenterY, native.systemFont, 44 )

playButton:setFillColor( 0.82, 0.86, 1 )

playButton:addEventListener( “tap”, gotoGame )

end

function scene:show( event )

local sceneGroup = self.view

local phase = event.phase

if phase == “will” then

– Called when the scene is still off screen and is about to move on screen

elseif phase == “did” then

– Called when the scene is now on screen

– 

– INSERT code here to make the scene come alive

– e.g. start timers, begin animation, play audio, etc.

end

end

function scene:hide( event )

local sceneGroup = self.view

local phase = event.phase

if event.phase == “will” then

– Called when the scene is on screen and is about to move off screen

– INSERT code here to pause the scene

– e.g. stop timers, stop animation, unload sounds, etc.)

elseif phase == “did” then

– Called when the scene is now off screen

end

end

function scene:destroy( event )

local sceneGroup = self.view

– Called prior to the removal of scene’s “view” (sceneGroup)

– 

– INSERT code here to cleanup the scene

– e.g. remove display objects, remove touch listeners, save state, etc.

if playBtn then

playBtn:removeSelf() – widgets must be manually removed

playBtn = nil

end

end


– Listener setup

scene:addEventListener( “create”, scene )

scene:addEventListener( “show”, scene )

scene:addEventListener( “hide”, scene )

scene:addEventListener( “destroy”, scene )


return scene

Thank you.

As far as the button not removing. It looks like you named the object playButton, but you attempt to do “playBtn:removeSelf()”

As far as the physics error. Please edit your post to use code tags so we can read it better.

As far as the button not removing. It looks like you named the object playButton, but you attempt to do “playBtn:removeSelf()”

As far as the physics error. Please edit your post to use code tags so we can read it better.