Try again menu takes me back to the game but doubles game speed and doesn't start scene from beginning

I have a small game in which the goal is to avoid rocks. If the player touches a rock, they go to a try again scene, if they tap on the try again button they get sent back to the game, but instead of starting over they are put back where they left off and touch the rock again immediately taking them back to the try again menu and eventually crashing. 

Other times when you tap in the try again menu the game start off with the game character jumping and everything is sped up in the game.

Not sure what I am doing wrong.

I want users to touch the try again screen and get take back to the game but have the game start over, not start where they left off.

here is my code: 

Game

----------------------------------------------------------------------------------------- -- Level -- main.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 -- Initialize the scene here. -- Example: add display objects to "sceneGroup", add touch listeners, etc. --Image Assets bg = display.newImageRect("images/GameBackground.png",display.viewableContentHeight\*2,display.viewableContentWidth\*2) bg.x= display.viewableContentWidth/2 bg.y = display.viewableContentHeight/2 bg.anchorX = 0.5 bg.anchorY = 0.5 physics = require("physics") physics.start() physics.setDrawMode( "normal" ) sceneGroup:insert(bg) bg2 = display.newImageRect("images/GameBackground.png",display.viewableContentHeight\*2,display.viewableContentWidth\*2) bg2.x= display.viewableContentWidth\*4 bg2.y = display.viewableContentHeight/2 bg2.anchorX = 0.5 bg2.anchorY = 0.5 sceneGroup:insert(bg2) ground = display.newRect(display.viewableContentWidth/2,display.viewableContentHeight-84,display.viewableContentWidth\*3.5, 65) ground.anchorX = 0.5 ground.anchorY = 0.5 ground:setFillColor(0,0,0) physics.addBody( ground, "static", { density=3.0, friction=0.5, bounce=0.1} ) sceneGroup:insert(ground) ghost = display.newImageRect("images/Ghost.png", 83,55) ghost.y = display.viewableContentHeight-170 physics.addBody( ghost, "dynamic", { density=3, friction=5.5, bounce=.3 } ) ghost.isFixedRotation = true sceneGroup:insert(ghost) stoned = {"images/Bones\_Gravestone\_nograss.png", "images/Skull\_Gravestone\_nograss.png", "images/Web\_Gravestone\_nograss.png"} --Stones stone1 = display.newImageRect(stoned[math.random(1,3)], 50,50) stone1.x = display.viewableContentWidth \* 2.2 stone1.anchorX = 0.5 stone1.anchorY = 0.5 stone1.y = (ground.y - stone1.height-3) sceneGroup:insert(stone1) stone2 = display.newImageRect(stoned[math.random(1,3)], 50,50) stone2.x = display.viewableContentWidth \* 3.2 stone2.anchorX = 0.5 stone2.anchorY = 0.5 stone2.y = (ground.y - stone2.height-3) sceneGroup:insert(stone2) stone3 = display.newImageRect(stoned[math.random(1,3)], 50,50) stone3.x = display.viewableContentWidth \* 5.2 stone3.anchorX = 0.5 stone3.anchorY = 0.5 stone3.y = (ground.y - stone2.height-3) sceneGroup:insert(stone3) stone4 = display.newImageRect(stoned[math.random(1,3)], 50,50) stone4.x = display.viewableContentWidth \* 7.2 stone4.anchorX = 0.5 stone4.anchorY = 0.5 stone4.y = (ground.y - stone2.height-3) sceneGroup:insert(stone4) stone5 = display.newImageRect(stoned[math.random(1,3)], 50,50) stone5.x = display.viewableContentWidth \* 8.5 stone5.anchorX = 0.5 stone5.anchorY = 0.5 stone5.y = (ground.y - stone2.height-3) sceneGroup:insert(stone5) --Grass grass = display.newImageRect("images/Grass.png", display.viewableContentWidth\*5, 200) grass.anchorX = .5 grass.y = (display.viewableContentHeight - 171) sceneGroup:insert(grass) --Wall Left leftWall = display.newRect(display.viewableContentWidth-display.viewableContentWidth\*2.13, 200,100,display.viewableContentHeight) physics.addBody(leftWall,"kinematic", { density=10, friction=10, bounce=5 } ) leftWall.alpha=0 sceneGroup:insert(leftWall) rightWall = display.newRect(display.viewableContentWidth\*2.13, 200,100,display.viewableContentHeight) physics.addBody(rightWall,"kinematic", { density=10, friction=10, bounce=5 } ) rightWall.alpha=0 sceneGroup:insert(rightWall) --Coin coin = display.newImageRect("images/Coin.png", 45, 45) coin.x = display.viewableContentWidth\*2.5 coin.y = 350 physics.addBody( coin, "kinematic", { density=0, friction=0, bounce=0 } ) sceneGroup:insert(coin) coin2 = display.newImageRect("images/Coin.png", 45, 45) coin2.x = coin.x\*2.5 coin2.y = 350 physics.addBody( coin2, "kinematic", { density=0, friction=0, bounce=0 } ) sceneGroup:insert(coin2) --Score scoreNum = tonumber(0) score = display.newText(scoreNum, 0, 0, native.systemFont, 32) score.anchorX = .5 score.anchorY = .5 score.x = display.viewableContentWidth\*1.92 score.y = 50 sceneGroup:insert(score) --scoreIcon scoreIcon = display.newImageRect("images/Coin.png", 45, 45) scoreIcon.y = score.y-2 scoreIcon.x = score.x - 50 sceneGroup:insert(scoreIcon) --Stone Physics physics.addBody( stone1, "static", { density=3, friction=5.5, bounce=.3 } ) physics.addBody( stone2, "static", { density=3, friction=5.5, bounce=.3 } ) physics.addBody( stone3, "static", { density=3, friction=5.5, bounce=.3 } ) physics.addBody( stone4, "static", { density=3, friction=5.5, bounce=.3 } ) physics.addBody( stone5, "static", { density=3, friction=5.5, bounce=.3 } ) -- Listener Functions 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). --Start --End elseif ( phase == "did" ) then -- Called when the scene is now on screen. -- Insert code here to make the scene come alive. -- Example: start timers, begin animation, play audio, etc. --Scrolls bg function scrollBG(self, event) if self.x \< -(display.viewableContentWidth\*3) then self.x = display.viewableContentWidth\*4 else self.x = self.x - 3 end end function scrollBG2(self, event) if self.x \< -(display.viewableContentWidth\*3) then self.x = display.viewableContentWidth\*4 else self.x = self.x - 3 end end -- Jump Function function jump(event) if event.phase == "began" and ghost.y \>= (display.viewableContentHeight-200) then ghost:applyForce(200, -4500, ghost.x, ghost.y) end return true end --Scroll Tomb Stone function scrollStone(self, event) if self.x \< -(display.viewableContentWidth\*3) then self.x = display.viewableContentWidth\*math.random(2,5) else self.x = self.x - 5 end end --Scroll Coin function scrollCoin(self, event) if self.x \< -(display.viewableContentWidth\*3) then self.x = display.viewableContentWidth\*math.random(2,5) self.y = math.random(300, 400) physics.addBody(coin, "kinematic", { density=0, friction=0, bounce=0 }) transition.to(coin, {time=300, alpha=100, width=45, height=45}) else self.x = self.x - 5 end end --Scroll Coin function scrollCoin2(self, event) if self.x \< -(display.viewableContentWidth\*3) then self.x = display.viewableContentWidth\*math.random(2,5) self.y = math.random(300, 400) physics.addBody(coin2,"kinematic", { density=0, friction=0, bounce=0 }) transition.to(coin2, {time=300, alpha=100, width=45, height=45}) else self.x = self.x - 5 end end function removeTheBody() physics.removeBody(coin) end local function removeTheBody2() physics.removeBody(coin2) end --Coin Collision function onCoinCollision( self, event ) transition.to(coin, {time=300, alpha=0, width=10, height=30}) scoreNum = scoreNum+1 score.text = scoreNum timer.performWithDelay( 1, removeTheBody ) end function onCoin2Collision( self, event ) transition.to(coin2, {time=300, alpha=0, width=10, height=30}) scoreNum = scoreNum+1 score.text = scoreNum timer.performWithDelay( 1, removeTheBody2 ) end function onStoneCollision() local options = { effect = "crossFade", time = 800, params = { level="Level 1", score=currentScore } } composer.gotoScene("tryAgain", options) end --Tomb Stoned --Event Listeners bg.enterFrame=scrollBG Runtime:addEventListener("enterFrame", bg) bg2.enterFrame=scrollBG2 Runtime:addEventListener("enterFrame", bg2) stone1.enterFrame=scrollStone Runtime:addEventListener("enterFrame", stone1) stone2.enterFrame=scrollStone Runtime:addEventListener("enterFrame", stone2) stone3.enterFrame=scrollStone Runtime:addEventListener("enterFrame", stone3) stone4.enterFrame=scrollStone Runtime:addEventListener("enterFrame", stone4) stone5.enterFrame=scrollStone Runtime:addEventListener("enterFrame", stone5) --Ghost Listener Runtime:addEventListener("touch", jump) --Coin Move coin.enterFrame=scrollCoin Runtime:addEventListener("enterFrame", coin) coin.collision=onCoinCollision coin:addEventListener("collision", coin) --Coin Move coin2.enterFrame=scrollCoin2 Runtime:addEventListener("enterFrame", coin2) coin2.collision=onCoin2Collision coin2:addEventListener("collision", coin2) stone1.collision=onStoneCollision stone1:addEventListener("collision", stone1) stone2.collision=onStoneCollision stone2:addEventListener("collision", stone2) stone3.collision=onStoneCollision stone3:addEventListener("collision", stone3) stone4.collision=onStoneCollision stone4:addEventListener("collision", stone4) stone5.collision=onStoneCollision stone5:addEventListener("collision", stone5) --Start --End 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

TryAgain

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 -- Initialize the scene here. -- Example: add display objects to "sceneGroup", add touch listeners, etc. local bg = display.newImageRect("images/TryAgain.png",display.viewableContentHeight\*2,display.viewableContentWidth\*2) bg.x= display.viewableContentWidth/2 bg.y = display.viewableContentHeight/2 bg.anchorX = 0.5 bg.anchorY = 0.5 sceneGroup:insert(bg) function tapper(event) local options = { effect = "crossFade", time = 800, params = { level="Level 1", score=currentScore } } composer.gotoScene("main2", options) print(55) end bg:addEventListener("tap",tapper) 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 -- Called when the scene is now on screen. -- Insert code here to make the scene come alive. -- Example: start timers, begin animation, play audio, etc. 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. composer.removeScene("tryAgain", true ) end -- ------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ------------------------------------------------------------------------------- return scene

You are adding event listeners in show and not clearing them when you move to another scene, so when you come back to your game scene, you are adding a second listener.  The build up of listeners over and over again will do funky things to your app.

Also, you might want to seriously consider reformatting the code… you should not place all your game logic inside the scene functions. 

The create scene should do enough to create the scene.

The show scene should do enough to show the scene.

etc.

The actual game play functions should be in your lua file, but outside the scene functions.

If you look at some Corona examples, you will get the basic idea.

–John 

You are adding event listeners in show and not clearing them when you move to another scene, so when you come back to your game scene, you are adding a second listener.  The build up of listeners over and over again will do funky things to your app.

Also, you might want to seriously consider reformatting the code… you should not place all your game logic inside the scene functions. 

The create scene should do enough to create the scene.

The show scene should do enough to show the scene.

etc.

The actual game play functions should be in your lua file, but outside the scene functions.

If you look at some Corona examples, you will get the basic idea.

–John