Purge scene not working properly

I am trying to make my purge scene work when I got to the start a next level screen but the only thing that changes accordingly is my background . The text that says touch screen to start next level doesn’t remove itself . this is my code for game .lua:

-- requires local physics = require "physics" physics.start() local storyboard = require( "storyboard" ) local scene = storyboard.newScene() -- background function scene:createScene(event) local screenGroup = self.view display.setDefault("background", 1, 3, 2) function SetDefaultAnchor( pos ) display.setDefault("anchorX", pos.x) display.setDefault("anchorY", pos.y) end function CreateWalls(BorderWidth) -- make the math easier local OldAnchor = {x = display.getDefault(anchorX), y = display.getDefault(anchorY) } SetDefaultAnchor({x=0, y=0}) local Height = display.contentHeight -- + (2 \* BorderWidth) local Width = display.contentWidth -- + (2 \* BorderWidth) local leftWall = display.newRect(0, 0, BorderWidth, Height) local rightWall = display.newRect(Width - BorderWidth, 0, BorderWidth, Height) local ceiling = display.newRect(0, 0, Width, BorderWidth) local floor = display.newRect(0, Height-BorderWidth, Width, BorderWidth) physics.addBody (leftWall, "static", {bounce = 0.7, friction = 2}) physics.addBody (rightWall, "static", {bounce = 0.0, friction = 2}) physics.addBody (ceiling, "static", {bounce = 0.8, friction = 2}) physics.addBody (floor, "static", {bounce = 0.0, friction = 2}) -- restore previous defaults SetDefaultAnchor(OldAnchor) end physics.start() -- let's make stuff yellow display.setDefault("fillColor", 0, 1, 1) CreateWalls(1) -- create image of a ball Ball = display.newCircle(100, 100, 10) Ball:addEventListener( "tap", onBallTap ) screenGroup:insert( Ball ) -- define physics object attached to image of ball physics.addBody(Ball, "dynamic", {friction=2}) -- give ball a shove Ball:applyLinearImpulse(-.05, .05, 0, 0) end function onBallTap( event ) storyboard.gotoScene("restart", "fade", 400) return true end function scene:enterScene(event) storyboard.purgeScene("restart") end function scene:exitScene(event) Ball:removeEventListener( "tap", onBallTap ) end function scene:destroyScene(event) end scene:addEventListener("createScene", scene) scene:addEventListener("enterScene", scene) scene:addEventListener("exitScene", scene) scene:addEventListener("destroyScene", scene) return scene

restart.lua(where the problem is)

-- requires local storyboard = require( "storyboard" ) local scene = storyboard.newScene() -- background function scene:createScene(event) local screenGroup = self.view myText = display.newText( "Touch Screen", 20, 100, native.systemFont, 26 ) myText:setFillColor( 0, 1, 1 ) myText = display.newText( "To Start Next Level", 20, 130, native.systemFont, 26 ) myText:setFillColor( 0, 1, 1 ) display.setDefault("background", 0, 0, 1) end function touchScreen( event ) if event.phase == "began" then storyboard.purgeScene("game2") storyboard.gotoScene("game2", "fade", 400) end end function scene:enterScene(event) Runtime:addEventListener("touch", touchScreen) end function scene:exitScene(event) storyboard.purgeScene("restart") Runtime:removeEventListener("touch", touchScreen) end function scene:destroyScene(event) end scene:addEventListener("createScene", scene) scene:addEventListener("enterScene", scene) scene:addEventListener("exitScene", scene) scene:addEventListener("destroyScene", scene) return scene

You’re not inserting all objects into ‘screenGroup’

If you don’t insert them, the scene doesn’t automatically manage them.

For example this is wrong:

myText = display.newText( "Touch Screen", 20, 100, native.systemFont, 26 )

This is right:

local myText = display.newText( screenGroup, "Touch Screen", 20, 100, native.systemFont, 26 )

or

local myText = display.newText( "Touch Screen", 20, 100, native.systemFont, 26 ) screenGroup:insert(myText)

Note: You’re making a lot of globals too.  Be careful about that.

it still doesn’t work

at restart.lua put this at enter:scene
[lua]
storyboard.removeScene (“game.lua”)
[/lua]

at game.lua put this at enter:scene
[lua]
storyboard.removeScene(“restart.lua”)
[/lua]

You’re not inserting all objects into ‘screenGroup’

If you don’t insert them, the scene doesn’t automatically manage them.

For example this is wrong:

myText = display.newText( "Touch Screen", 20, 100, native.systemFont, 26 )

This is right:

local myText = display.newText( screenGroup, "Touch Screen", 20, 100, native.systemFont, 26 )

or

local myText = display.newText( "Touch Screen", 20, 100, native.systemFont, 26 ) screenGroup:insert(myText)

Note: You’re making a lot of globals too.  Be careful about that.

it still doesn’t work

at restart.lua put this at enter:scene
[lua]
storyboard.removeScene (“game.lua”)
[/lua]

at game.lua put this at enter:scene
[lua]
storyboard.removeScene(“restart.lua”)
[/lua]