Overlay problems :(

I was trying to reload the game scene after the game over overlay is gone, trying to go for a Ketchapp style game, where the best score and game over buttons  on the overlay scene. The problem i’m having is on every reload an extra ball spawns with the original… Any help is very appreciated. I will attach all the stuff for you to see. I’m also looking for a side teacher to help me over those speed bumps making these games, willing to pay for some guidance. Add me on skype @austin_rau! I have tons of Ideas that will make a ton of money and build a huge audience. I just have to get my these scene bumps first lol. This game is a simple pong type where you have to keep it up! 

GAME.LUA

local storyboard = require( “storyboard” )

local scene = storyboard.newScene()

 --Require gamenetwork

    local gameNetwork = require( “gameNetwork” )

    --Require physics

    local physics = require(“physics”)

    physics.start()

    physics.setGravity(0,50)

    physics.setDrawMode( “normal” )

    --Require data

    local mydata = require( “mydata” )

    mydata.score = 0

    --Gotta have these nifty buggers

    cw = display.contentWidth

    ch = display.contentHeight 

    xPositions = {cw*.4,cw*.5,cw*.6}

    mRandom = math.random

  

   local ball

   local box

    local pointSFX

    local goSFX

    local clickSFX

    audio.setVolume( 0.20, { channel=1 } ) – set the volume on channel 1

    audio.setVolume( 0.30, { channel=2 } ) – set the volume on channel 2

    local gameStarted = false

   

function scene:createScene( event )

        local sceneGroup = self.view

 pointSFX = audio.loadSound(“SFX/Pop.mp3”);

        optionsPointSFX =

{

    channel = 1,

    fadein = 500,

    

}

    goSFX = audio.loadSound(“SFX/gameover.wav”);

       optionsGoSFX =

{

    channel = 2,

    fadein = 500,

    

}

    clickSFX = audio.loadSound(“SFX/lighttap.mp3”)

    local sceneGroup = self.view

    local background = display.newRect(0,0,cw,ch) background:setFillColor(253/255,253/255,253/255)

            background.y = ch/2

            background.x = cw/2

            sceneGroup:insert(background)

    local hand = display.newImage(“Images/hand.png”)

            hand:scale(.7,.7)

            hand.x = cw*.58

            hand.y = ch*.95

    local leftCorner = display.newImage(“Images/LTcorner.png”) leftCorner:scale(.5,.5)

            leftCorner.anchorX = 0

            leftCorner.anchorY = 0

            leftCorner.x = 0

            leftCorner.y = 0

    local rightCorner = display.newImage(“Images/RTcorner.png”) rightCorner:scale(.5,.5)

            rightCorner.anchorX = 1

            rightCorner.anchorY = 0

            rightCorner.x = cw

            rightCorner.y = 0

    local rightCornerB = display.newImage(“Images/RTcornerB.png”) rightCornerB:scale(.5,.5)

            rightCornerB.anchorX = 1

            rightCornerB.anchorY = 1

            rightCornerB.x = cw

            rightCornerB.y = ch

    local leftCornerB = display.newImage(“Images/LTcornerB.png”) leftCornerB:scale(.5,.5)

            leftCornerB.anchorX = 0

            leftCornerB.anchorY = 1

            leftCornerB.x = 0

            leftCornerB.y = ch

wallBounce = 1

wallFriction = 0

leftWall = display.newRect(0,0,50,ch)

leftWall.anchorX = 1

leftWall.anchorY = 0.5

leftWall.x = 0

leftWall.y = ch/2

physics.addBody(leftWall, “static”, {isSensor=false,density=1, bounce=wallBounce, friction=wallFriction})

rightWall = display.newRect(0,0,50,ch)

rightWall.anchorX = 0.0

rightWall.anchorY = 0.5

rightWall.x = cw

rightWall.y = ch/2

physics.addBody(rightWall, “static”, {isSensor=false,density=1, bounce=wallBounce, friction=wallFriction})

topWall = display.newRect(0,0,cw,50)

topWall.anchorX = 0.5

topWall.anchorY = 1

topWall.x = cw/2

topWall.y = 0

physics.addBody(topWall, “static”, {isSensor=false,density=1, bounce=wallBounce, friction=wallFriction})

floor = display.newRect(0,0,cw,50)

floor.anchorX = 0.5

floor.anchorY = 0.0

floor.x = cw/2

floor.y = ch

physics.addBody(floor, “static”, {isSensor=false,density=1, bounce=wallBounce, friction=wallFriction})       

line = display.newRect(100,100,cw,10) line:setFillColor(230/255,230/255,230/255)

            line.x = cw/2

            line.y = ch*.7

            line.isFixedRotation = true

            sceneGroup:insert(line)

            --ball

           

        

            box = display.newRect(100,100,100,30) box:setFillColor(200/255,0/255,50/255)

            box.x = 100

            box.y = ch*.7

            physics.addBody(box, “static”, {density=100,friction=10,bounce=1,radius=radius, isSensor=false})

            box.isFixedRotation = true

            sceneGroup:insert(box)

            --box:setLinearVelocity(0,0)

    --Score

        local scoreText = display.newText(mydata.score,display.contentCenterX,

            ch*.2, “Futura”, 100)

            scoreText:setFillColor(210/255,210/255,210/255)

            scoreText.alpha = 1

            sceneGroup:insert(scoreText)

    --Score

    --Floor

    local floor = display.newRect (0,0,cw,100)

        floor.anchorX = 0.5

        floor.anchorY = 0.5

        floor.x = cw/2

        floor.y = ch*.8

        floor.alpha = 0

                        physics.addBody(floor, “static”, {density=1,friction=1,radius=radius, isSensor=true})

local function floorCollision( event )

   if(event.phase == “began”) then

    event.other:removeSelf()

    gameOver()

   

    --ball = nil

    print(“floor hit”)

    end

end

floor:addEventListener(“collision”, floorCollision)

function boxCollision( event )

   if(event.phase == “ended”) then

    

    mydata.score = mydata.score + 1

    --ball = nil

    print(“box hit”)

    end

end

box:addEventListener(“collision”, boxCollision)

            --gameOver

            function gameOver(event)

                        audio.play(goSFX, { optionsGoSFX })

                gameStarted = false

                local options =

                {

                    isModal = true,

                    effect = “fromRight”,

                    time = 1500

                }

                storyboard.showOverlay( “end”, options )

                                    return true

            end

        --Collision

        --Speed up from start

            local function speedUp(e)

                print(“speeding up”)

                speedLevel = speedLevel + 1

            end

        --Every frame

       switch = true

       boxSpeed = 600

            

            function everyFrame( event )

                

               

                                    scoreText.text = mydata.score

            end

        --Every frame

        --Touched

        function onTouch( event )

            print(“touch”)

            if (switch == true and event.phase == “ended”) then

                transition.to( box, { time=boxSpeed ,x=cw-100 ,y=box.y } )

                switch = false

            elseif (switch == false and event.phase == “ended”) then

                switch = true

                transition.to( box, { time=boxSpeed ,x=100 ,y=box.y } )

            end

        end

        --Touched

        --Event listeners

            Runtime:addEventListener(“touch”, onTouch)

            Runtime:addEventListener(“enterFrame”, everyFrame)

         hand.alpha = 0

        function gameStart(event)

    if (gameStarted == false) then

         audio.play(clickSFX)

         hand.alpha = 0

         gameStarted = true

    end

end

end

– Called BEFORE scene has moved onscreen:

function scene:willEnterScene( event )

        local sceneGroup = self.view

    mydata.score = 0

    --createBall()

        -----------------------------------------------------------------------------

        –      This event requires build 2012.782 or later.

        -----------------------------------------------------------------------------

end

– Called immediately after scene has moved onscreen:

function scene:enterScene( event )

        local sceneGroup = self.view

        -------------------

         

              

            

            ball = display.newCircle(0,0,25) ball:setFillColor(200/255,200/255,200/255)

            ball.x = cw/2

            ball.y = ch*.2

            physics.addBody(ball, “dynamic”, {density=.1,friction=10,bounce=1,radius=25, isSensor=false})

            sceneGroup:insert(ball)


        –      INSERT code here (e.g. start timers, load audio, start listeners, etc.)

        -----------------------------------------------------------------------------

end

– Called when scene is about to move offscreen:

function scene:exitScene( event )

        local group = self.view

        -----------------------------------------------------------------------------

        –      INSERT code here (e.g. stop timers, remove listeners, unload sounds, etc.)

        -----------------------------------------------------------------------------

end

– Called AFTER scene has finished moving offscreen:

function scene:didExitScene( event )

        local sceneGroup = self.view

        -----------------------------------------------------------------------------

        –      This event requires build 2012.782 or later.

        -----------------------------------------------------------------------------

end

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

function scene:destroyScene( event )

        local sceneGroup = self.view

        -----------------------------------------------------------------------------

        –      INSERT code here (e.g. remove listeners, widgets, save state, etc.)

        -----------------------------------------------------------------------------

end

– Called if/when overlay scene is displayed via storyboard.showOverlay()

function scene:overlayBegan( event )

        local group = self.view

        local overlay_name = event.sceneName  – name of the overlay scene

        -----------------------------------------------------------------------------

        –      This event requires build 2012.797 or later. 

        box.alpha = .2

        line.alpha = .2

        -----------------------------------------------------------------------------

end

– Called if/when overlay scene is hidden/removed via storyboard.hideOverlay()

function scene:overlayEnded( event )

        local group = self.view

        local overlay_name = event.sceneName  – name of the overlay scene

        -----------------------------------------------------------------------------

        box.alpha = 1

        line.alpha = 1

        –      This event requires build 2012.797 or later.

        -----------------------------------------------------------------------------

end


– END OF YOUR IMPLEMENTATION


– “createScene” event is dispatched if scene’s view does not exist

scene:addEventListener( “createScene”, scene )

– “willEnterScene” event is dispatched before scene transition begins

scene:addEventListener( “willEnterScene”, scene )

– “enterScene” event is dispatched whenever scene transition has finished

scene:addEventListener( “enterScene”, scene )

– “exitScene” event is dispatched before next scene’s transition begins

scene:addEventListener( “exitScene”, scene )

– “didExitScene” event is dispatched after scene has finished transitioning out

scene:addEventListener( “didExitScene”, scene )

– “destroyScene” event is dispatched before view is unloaded, which can be

– automatically unloaded in low memory situations, or explicitly via a call to

– storyboard.purgeScene() or storyboard.removeScene().

scene:addEventListener( “destroyScene”, scene )

– “overlayBegan” event is dispatched when an overlay scene is shown

scene:addEventListener( “overlayBegan”, scene )

– “overlayEnded” event is dispatched when an overlay scene is hidden/removed

scene:addEventListener( “overlayEnded”, scene )


return scene

END.LUA


– BEGINNING OF YOUR IMPLEMENTATION


– Called when the scene’s view does not exist:

function scene:createScene( event )

        local group = self.view

    local replay = display.newImage( “Images/replayButton.png”, 800, 410 )

    replay:scale(.4,.4)

          replay.anchorX = 0.5

          replay.anchorY = 0.5

          replay.x = cw/2

          replay.y = ch/2

          group:insert(replay)

          function restart( event )

            storyboard.hideOverlay( “fade”, 400 )

                 storyboard.gotoScene(“game”)

          end

replay:addEventListener( “touch”, restart )

bestText = score.init({

    fontSize = 100,

    font = “Futura”,

    0,0,

    maxDigits = 3,

    leadingZeros = false,

    filename = “scorefile.txt”,

    }) 

    bestScore = score.get()

    bestText.anchorX = 0.5

    bestText.anchorY = 0.5

    bestText.x = cw/2

    bestText.y = ch*.35

    bestText.text = bestScore

    bestText.alpha = 1

    bestText:setFillColor(50/255,20/255,20/255)

    group:insert(bestText)

end

– Called BEFORE scene has moved onscreen:

function scene:willEnterScene( event )

        local group = self.view

end

– Called immediately after scene has moved onscreen:

function scene:enterScene( event )

        local group = self.view

end

– Called when scene is about to move offscreen:

function scene:exitScene( event )

        local group = self.view

end

– Called AFTER scene has finished moving offscreen:

function scene:didExitScene( event )

     

end

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

function scene:destroyScene( event )

end

– Called if/when overlay scene is displayed via storyboard.showOverlay()

function scene:overlayBegan( event )

        local group = self.view

        local overlay_name = event.sceneName  – name of the overlay scene

        -----------------------------------------------------------------------------

        –      This event requires build 2012.797 or later.

        -----------------------------------------------------------------------------

end

– Called if/when overlay scene is hidden/removed via storyboard.hideOverlay()

function scene:overlayEnded( event )

        local group = self.view

        local overlay_name = event.sceneName  – name of the overlay scene

       print(“Sihajsdjhj”)

end


– END OF YOUR IMPLEMENTATION


– “createScene” event is dispatched if scene’s view does not exist

scene:addEventListener( “createScene”, scene )

– “willEnterScene” event is dispatched before scene transition begins

scene:addEventListener( “willEnterScene”, scene )

– “enterScene” event is dispatched whenever scene transition has finished

scene:addEventListener( “enterScene”, scene )

– “exitScene” event is dispatched before next scene’s transition begins

scene:addEventListener( “exitScene”, scene )

– “didExitScene” event is dispatched after scene has finished transitioning out

scene:addEventListener( “didExitScene”, scene )

– “destroyScene” event is dispatched before view is unloaded, which can be

– automatically unloaded in low memory situations, or explicitly via a call to

– storyboard.purgeScene() or storyboard.removeScene().

scene:addEventListener( “destroyScene”, scene )

– “overlayBegan” event is dispatched when an overlay scene is shown

scene:addEventListener( “overlayBegan”, scene )

– “overlayEnded” event is dispatched when an overlay scene is hidden/removed

scene:addEventListener( “overlayEnded”, scene )


return scene

Just taking a ‘quick look’ here, it seems that on scene:show  … each time scene:show is called you are creating a new ‘circle’, and assigning that circle to the the ‘ball’ variable/handle 

Create the ball object in the scene:create function 

Also, it will be worth your time to start over, using Composer instead of storyboard.  Composer is the new improved (and supported) version.  It is very similar and should deb quick to pick up.

The thing about learning this stuff, is be prepared to spend a lot of time typing and re-typing and re-typing code as you learn this …

here is are some links to Composer stuff :

http://coronalabs.com/blog/2014/01/21/introducing-the-composer-api-plus-tutorial/

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

Hope this helps!

Is there anywhere I could message you?

cyberparkstudios@gmail.com

Just taking a ‘quick look’ here, it seems that on scene:show  … each time scene:show is called you are creating a new ‘circle’, and assigning that circle to the the ‘ball’ variable/handle 

Create the ball object in the scene:create function 

Also, it will be worth your time to start over, using Composer instead of storyboard.  Composer is the new improved (and supported) version.  It is very similar and should deb quick to pick up.

The thing about learning this stuff, is be prepared to spend a lot of time typing and re-typing and re-typing code as you learn this …

here is are some links to Composer stuff :

http://coronalabs.com/blog/2014/01/21/introducing-the-composer-api-plus-tutorial/

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

Hope this helps!

Is there anywhere I could message you?

cyberparkstudios@gmail.com