problem with game scene

When I click my ball , i want to go to the restart screen but when I go there the ball from the game scene is there because the scene didn’t purge properly . I also have a function if the user doesn’t touch the ball in the given time then they will go to a screen but that doesn’t work . 

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 ) -- 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 local timeLimit = 20 timeLeft = display.newText(timeLimit, 160, 20, native.systemFontBold, 24) timeLeft:setTextColor(255,0,0) function timerDown() timeLimit = timeLimit-1 timeLeft.text = timeLimit if(timeLimit==0)then storyboard.gotoScene("maxtime", "fade", 400) end end timer.performWithDelay(1000,timerDown,timeLimit) function onBallTap( event ) storyboard.purgeScene("restart") storyboard.gotoScene("restart", "fade", 400) return true end function scene:enterScene(event) Ball:addEventListener( "tap", onBallTap ) end function scene:exitScene(event) Ball:removeEventListener( "tap", onBallTap ) storyboard.purgeScene("maxtime") end function scene:destroyScene(event) end scene:addEventListener("createScene", scene) scene:addEventListener("enterScene", scene) scene:addEventListener("exitScene", scene) scene:addEventListener("destroyScene", scene) return scene

maxtime.lua 

local storyboard = require( "storyboard" ) local scene = storyboard.newScene() display.setDefault("background", 1, 3, 2) return scene

restart.lua 

-- 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 = display.newText( "To Play next level", 20, 120, native.systemFont, 26 ) display.setDefault("background", 0, 0, 1) end function touchScreen( event ) if event.phase == "began" then 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

Any Help ?

I don’t know much about storyboard, but you should migrant to composer. I have heard that there are some bugs with storyboard.

https://docs.coronalabs.com/api/library/composer/migration.html

Do you have storyboard installed?
https://github.com/coronalabs/framework-storyboard-legacy

I’m more comfortable with storyboard and yes it’s installed

You need to add your display object’s to the scene’s group (screenGroup)

I tried before but it didn’t work . I’ll try again now

I don’t know much about storyboard, but you should migrant to composer. I have heard that there are some bugs with storyboard.

https://docs.coronalabs.com/api/library/composer/migration.html

Do you have storyboard installed?
https://github.com/coronalabs/framework-storyboard-legacy

I’m more comfortable with storyboard and yes it’s installed

You need to add your display object’s to the scene’s group (screenGroup)

I tried before but it didn’t work . I’ll try again now