Storyboard not working on endGame?

Can someone please help me, I have a game, and a character that has 3 lives, when lives is = to 0, I want to go to a restart.lua file. The below code, works but the restart.lua file appears in the background, and the game.lua is still visible and not purged?

Any help would be great!

Thanks

Anthony

local lives = 3 -- Set Lives to 3 --display lives on screen local playerLives = display.newText("Lives: "..lives, 20, 10, native.systemFont, 15); playerLives:setTextColor(0, 119, 112); playerLives.x = 320 --calc lives function CalcLife() if (lives \> 0) then lives = lives -1 if (lives \> 0) then playerLives.text = "Lives: " .. lives audio.play(life\_lost) else endGame() end else endGame() end end function endGame () storyboard.gotoScene("restart", "fade", 6000) audio.stop(2) --stop audio rain channel=2 end function GameOver() local message1 = display.newText("GAME OVER", 20, 10, native.systemFont, 70) message1:setTextColor( 255, 255, 255 ) message1.x = screenW / 2 message1.y = screenH / 2 audio.play(gameOverScreen) end

Did you enter your display objects into the group view? That would keep the objects from being removed correctly.

Hi I have all objects in create scene? local screenGroup = self.view ?

can you provide your entire scene function? That would help to identify where the problem is occuring.

Where should I put the code so it’s removed correctly? I have a menu.lua and that works fine using storyboard.purgeAll() and storyboard.removeAll () works?

I’ll upload the code shortly thank you for your help really appreciate it :wink:

--\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* -- -- game.lua -- --\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* --\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* --Requires-- --\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* local storyboard = require( "storyboard" ) local scene = storyboard.newScene() local physics = require "physics" physics.start() local widget = require "widget" --\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* --Varibales --\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* local score = 0 -- Set Score to 10 local lives = 3 -- Set Lives to 3 local message = "" local playerInAir = true -- Set a boolean of whether our dog is in the air or not --Add sound local fuel\_cell = audio.loadSound("fuelcell.wav") -- Set sound variable local life\_lost = audio.loadSound("game\_over.wav") -- Set sound variable local gameOverScreen = audio.loadSound("game-over.wav") -- Set sound variable --\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* --BEGINNING OF GAME IMPLEMENTATION --\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* -- Called when the scene's view does not exist: function scene:createScene( event ) local screenGroup = self.view --------------------------------------------------------------------------------- --Add Rain --------------------------------------------------------------------------------- group = display.newGroup() local g = graphics.newGradient( { 100, 100, 100 }, { 100, 100, 100 }, "down" ) --local bg = display.newRect( 0, 0, screenW, screenH) --bg:setFillColor( g ) --bg.alpha = 0.5 --group:insert(bg) local rain = require("rain") rain.new(group, {}) --Pause rain: rain.pause() --Resume rain: rain.resume() --------------------------------------------------------------------------------- --End Rain --------------------------------------------------------------------------------- --------------------------------------------------------------------------------- -- Add Ceiling --------------------------------------------------------------------------------- local ceiling = display.newRect(0,-5,960,5) physics.addBody( ceiling, "static") group:insert( ceiling ) --------------------------------------------------------------------------------- -- --------------------------------------------------------------------------------- --------------------------------------------------------------------------------- --Add Backgrounds --------------------------------------------------------------------------------- local background = display.newImage("images/final/background-1.png") screenGroup:insert(background) background1 = display.newImage("images/final/background-1.png") background1:setReferencePoint(display.BottomLeftReferencePoint) background1.x = 0 background1.y = 320 background1.speed = 0.1 screenGroup:insert(background1) background2 = display.newImage("images/final/background-2.png") background2:setReferencePoint(display.BottomLeftReferencePoint) background2.x = 480 background2.y = 320 background2.speed = 0.1 screenGroup:insert(background2) landscapeback1 = display.newImage("images/final/landscape-back-1.png") landscapeback1:setReferencePoint(display.BottomLeftReferencePoint) landscapeback1.x = 0 landscapeback1.y = 320 landscapeback1.speed = 0.2 screenGroup:insert(landscapeback1) landscapeback2 = display.newImage("images/final/landscape-back-2.png") landscapeback2:setReferencePoint(display.BottomLeftReferencePoint) landscapeback2.x = 480 landscapeback2.y = 320 landscapeback2.speed = 0.2 screenGroup:insert(landscapeback2) landscapefront1 = display.newImage("images/final/landscape-front-1.png") landscapefront1:setReferencePoint(display.BottomLeftReferencePoint) landscapefront1.x = 0 landscapefront1.y = 320 landscapefront1.speed = 0.8 screenGroup:insert(landscapefront1) landscapefront2 = display.newImage("images/final/landscape-front-2.png") landscapefront2:setReferencePoint(display.BottomLeftReferencePoint) landscapefront2.x = 480 landscapefront2.y = 320 landscapefront2.speed = 0.8 screenGroup:insert(landscapefront2) floor1 = display.newImage("images/final/floorLargeBlue.png") floor1:setReferencePoint(display.BottomLeftReferencePoint) floor1.x = 0 floor1.y = 320 floor1.speed = 1.0 physics.addBody( floor1, "static") floor1.myName = "floor" --for collision screenGroup:insert(floor1) floor2 = display.newImage("images/final/floorLargeBlue.png") floor2:setReferencePoint(display.BottomLeftReferencePoint) floor2.x = 480 floor2.y = 320 floor2.speed = 1.0 physics.addBody( floor2, "static") floor2.myName = "floor" --for collision screenGroup:insert(floor2) --platform1 = display.newImage("images/final/platformBlueLarge.png") --platform1:setReferencePoint(display.BottomLeftReferencePoint) --platform1.x = 500 --platform1.y = 200 --platform1.speed = 1.0 --physics.addBody( platform1, "static") --screenGroup:insert(platform1) --platform2 = display.newImage("images/final/platformBlueMed.png") --platform2:setReferencePoint(display.BottomLeftReferencePoint) --platform2.x = 1000 --platform2.y = 150 --platform2.speed = 1.0 --physics.addBody( platform2, "static") --screenGroup:insert(platform2) --------------------------------------------------------------------------------- --Scroll Backgrounds --------------------------------------------------------------------------------- function scrollBackground(self,event) if self.x \< -477 then self.x = 480 else self.x = self.x - self.speed end end --------------------------------------------------------------------------------- --End Backgrounds --------------------------------------------------------------------------------- --------------------------------------------------------------------------------- -- Add Plant Enimies --------------------------------------------------------------------------------- --------------------------------------------------------------------------------- -- Add Alien Plant 1 --------------------------------------------------------------------------------- local function onTimer( event ) local obj = event.source.objectID obj:setLinearVelocity( -20, 0 ) end local AlienPlant = display.newImage( "images/final/alien-plant-large.png" ) AlienPlant.x = 700 --off screen AlienPlant.y = 236 --on floor physics.addBody( AlienPlant, "kinematic" ) AlienPlant:setLinearVelocity( 0, 0 ) AlienPlant.myName = "AlienPlant" --for collision local t = timer.performWithDelay( 250, onTimer ) ; t.objectID = AlienPlant --timer --------------------------------------------------------------------------------- -- Add Alien Plant 2 --------------------------------------------------------------------------------- local function onTimer( event ) local obj = event.source.objectID obj:setLinearVelocity( -20, 0 ) end local AlienPlant = display.newImage( "images/final/alien-plant-large.png" ) AlienPlant.x = 900 --off screen AlienPlant.y = 236 --on floor physics.addBody( AlienPlant, "kinematic" ) --or "dynamic" interact with object AlienPlant:setLinearVelocity( 0, 0 ) AlienPlant.myName = "AlienPlant" --for collision local t = timer.performWithDelay( 250, onTimer ) ; t.objectID = AlienPlant --timer --------------------------------------------------------------------------------- --End Plant Enimies --------------------------------------------------------------------------------- --------------------------------------------------------------------------------- -- Add Platforms --------------------------------------------------------------------------------- --------------------------------------------------------------------------------- -- Add Platforms1 --------------------------------------------------------------------------------- local function onTimer( event ) local obj = event.source.objectID obj:setLinearVelocity( -30, 0 ) --speed end local platform1 = display.newImage( "images/final/platformBlueMed.png" ) platform1.x = 1000 --off screen platform1.y = 200 --from floor physics.addBody( platform1 , "kinematic" ) --or "dynamic" interact with object platform1:setLinearVelocity( 0, 0 ) local t = timer.performWithDelay( 1100, onTimer ) ; t.objectID = platform1 --timer --------------------------------------------------------------------------------- -- Add Platforms2 --------------------------------------------------------------------------------- local function onTimer( event ) local obj = event.source.objectID obj:setLinearVelocity( -30, 0 ) --speed end local platform2 = display.newImage( "images/final/platformBlueMed.png" ) platform2.x = 1500--off screen platform2.y = 100 --from floor physics.addBody( platform2 , "kinematic" ) --or "dynamic" interact with object platform2:setLinearVelocity( 0, 0 ) local t = timer.performWithDelay( 1100, onTimer ) ; t.objectID = platform2 --timer --------------------------------------------------------------------------------- -- End Platforms --------------------------------------------------------------------------------- --------------------------------------------------------------------------------- -- Add Character Altazar to Game --------------------------------------------------------------------------------- -- Running Sprite local sheetOptions= { width=70, height=49, numFrames=9, sheetContentWidth=210, sheetContentHeight=147 } myLeftImageSheet = graphics.newImageSheet("images/dog-final-right.png", sheetOptions ) myRightImageSheet = graphics.newImageSheet("images/dog-final-left.png", sheetOptions ) myJumpRightImageSheet = graphics.newImageSheet( "images/dog-final-jump-left.png", sheetOptions) local sequenceData = { { name="leftRightRun", start=1, sheet=myLeftImageSheet, count=9, time = 1100}, --, loopCount = 2 { name="upJumpright", sheet= myJumpRightImageSheet, frames= {1,2,3,4,5,6,7,8,9}, time = 1100}, --, loopCount = 4 { name="rightLeftRun", sheet= myRightImageSheet, frames= {1,2,3,4,5,6,7,8,9}, time = 1100}, --loopCount = 2 } --now build the sprite to hold them and place it on the screen local dogAnimation = display.newSprite( myLeftImageSheet, sequenceData ) dogAnimation.x = screenW / 2 --enter 5 to position slightly of screen --the sprite position horizontally dogAnimation.y = screenH / 2 --the sprite position vertically --add physics physics.addBody( dogAnimation, "dynamic", { density=0.9, friction=0.5, bounce=0 } ) --dogAnimation:setLinearVelocity(300,100)-- change the velocity of the rigid body --dogAnimation:applyForce (100,100,100,100) --physics.setGravity (0.2, 0.2) dogAnimation.isFixedRotation = true dogAnimation.myName = "dogAnimation" --for collision dogAnimation:setLinearVelocity( 20,4 ) --play spritesheet dogAnimation:play() --dogAnimation:pause() --------------------------------------------------------------------------------- -- End Alatazar --------------------------------------------------------------------------------- --------------------------------------------------------------------------------- -- Add Enimies CAT and BIRD to Game --------------------------------------------------------------------------------- --Cat Running Sprite local sheetOptions= { width=100, height=70, numFrames=9, sheetContentWidth=300, sheetContentHeight=210 } --now use the options table to build 2 imageSheets- one for lright running --and the other for rleft running myCatImageSheet = graphics.newImageSheet("images/cat-final-left.png", sheetOptions ) -- add multiple sequences from these different sheets with different frame counts (for fast/slow) local sequenceData = { { name="catRight", start=1, sheet= myLeftImageSheet, count=9, time = 700,}, --, loopCount = 2 } --now build the sprite to hold them and place it on the screen local catAnimation = display.newSprite( myCatImageSheet, sequenceData ) catAnimation.x = 200--the sprite position horizontally catAnimation.y = screenH / 2 --the sprite position vertically --add physics physics.addBody( catAnimation, "dynamic", { density=0.2, friction=0.2, bounce=0.2 } ) --catAnimation:setLinearVelocity(300,100)-- change the velocity of the rigid body --catAnimation:applyForce (100,100,100,100) --physics.setGravity (0.2, 0.2) catAnimation.isFixedRotation = true catAnimation.myName = "catAnimation" --for collision --move cat catAnimation:translate(1000, -200) -- -200 above top of screen 400 left of screen transition.to(catAnimation, {x = -800, time = 10000}) --x = -800 run off screen to left --y = 200 --alpha = 0, catAnimation:play() --catAnimation:pause() --Bird Running Sprite local sheetOptions= { width=100, height=70, numFrames=5, sheetContentWidth=300, sheetContentHeight=140 } --now use the options table to build 2 imageSheets- one for lright running --and the other for rleft running --now use the options table to build 2 imageSheets- one for lright running --and the other for rleft running myBirdImageSheet = graphics.newImageSheet("images/bird-final-right.png", sheetOptions ) -- add multiple sequences from these different sheets with different frame counts (for fast/slow) local sequenceData = { { name="birdFlyingRight", start=1, sheet= myLeftImageSheet, count=5, time = 700}, --, loopCount = 2 { name="birdFlyingRight", sheet= myBirdImageSheet, frames= {1,2,3,4,5}, time = 250}, --, loopCount = 4 --, loopDirection = bounce } --now build the sprite to hold them and place it on the screen local birdAnimation = display.newSprite( myBirdImageSheet, sequenceData ) birdAnimation.x = -200--the sprite position horizontally birdAnimation.y = 160 --the sprite position vertically birdAnimation.rotation = -5 birdAnimation.myName = "birdAnimation" --for collision --move bird birdAnimation:translate(birdAnimation.width/2 , -110) transition.to(birdAnimation, {x = 700, time = 20000}) --y = 200 --alpha = 0, --add physics --physics.addBody( birdAnimation, "dynamic", { density=0.5, friction=0.5, bounce=0.3 } ) --birdAnimation:setLinearVelocity(300,100)-- change the velocity of the rigid body --birdAnimation:applyForce (100,100,100,100) --physics.setGravity (0.2, 0.2) --play spritesheet birdAnimation:play() --birdAnimation:pause() --------------------------------------------------------------------------------- -- --------------------------------------------------------------------------------- --------------------------------------------------------------------------------- -- Add Buttons --------------------------------------------------------------------------------- ---Control Alatazar Button functions local function buttonTouch(event) --take in the event and get the associated display object local buttonObj = event.target --now find out which button it is local nameString = buttonObj.id -- define a variable for the final x coordinate of the transition local finalX = dogAnimation.width/2 -- remember registration point is half way! --define another variable for the sequence to play, default left to right local spriteSequence = "leftRightRun" --now use a conditional to set left/right bounds for the transition --and also the animation sequence to load if nameString == "leftButton" then finalX = dogAnimation.width/2 -- default to left spriteSequence = "rightLeftRun" elseif nameString == "rightButton" then finalX = display.contentWidth - dogAnimation.width/2 -- right hand side of screen spriteSequence = "leftRightRun" elseif nameString == "upButton" then finalX = display.contentWidth - dogAnimation.width/2 -- right hand side of screen spriteSequence = "upJumpright" dogAnimation:applyLinearImpulse(0,-9,-9,0 ) end --with the final x value set (based on the button pressed) --the transition can be called after "began" phase if event.phase == "began" then --call the transition, now with added easing! transition.to(dogAnimation, {x = finalX, transition = easing.inCirc, time = 3000}) --was .outExpo 18000 --also load the relevant animation sequence and play it dogAnimation:setSequence( spriteSequence ) dogAnimation:play() elseif event.phase == "ended" then end --remember the issue with event propagation! return true end ---Control Alatazar Button --left button widget local leftButton = widget.newButton { defaultFile = "images/backDefault.png", overFile = "images/backOver.png", id = "leftButton", --id is unique and essential when using event handlers (see below) width = 64, height = 64, onEvent = buttonTouch, } --rightbutton widget local rightButton = widget.newButton{ id = "rightButton", --id is again unique defaultFile = "images/forwardDefault.png", overFile = "images/forwardOver.png", width = 64, height = 64, onEvent = buttonTouch, } --upbutton widget local upButton = widget.newButton{ id = "upButton", --id is again unique defaultFile = "images/upDefault.png", overFile = "images/upOver.png", width = 64, height = 64, onEvent = buttonTouch, } ---HUD Buttons --pausebutton widget local pauseButton = widget.newButton{ id = "pauseButton", --id is again unique defaultFile = "images/pauseDefault.png", overFile = "images/pauseOver.png", width = 30, height = 30, --onEvent = buttonTouch, } --resetbutton widget local resetButton = widget.newButton{ id = "resetButton", --id is again unique defaultFile = "images/resetDefault.png", overFile = "images/resetOver.png", width = 30, height = 30, --onEvent = buttonTouch, } --Positioning buttons leftButton.y = display.contentHeight - leftButton.height/2 rightButton.x = 120 rightButton.y = display.contentHeight - rightButton.height/2 upButton.x = display.contentWidth - rightButton.width/2 upButton.y = display.contentHeight - rightButton.height/2 --HUD pauseButton.x = screenW-460 pauseButton.y = 20 resetButton.x = 460 resetButton.y = 20 --------------------------------------------------------------------------------- -- --------------------------------------------------------------------------------- --------------------------------------------------------------------------------- -- Add HUD Score and Lives --------------------------------------------------------------------------------- --Add Score to Screen local playerScore = display.newText("Score: "..score, 20, 10, native.systemFont, 15); playerScore:setTextColor(255, 255, 255); playerScore.x = screenW / 2 --playerScore.y = display.contentHeight / 2 --Add lives to Screen local playerLives = display.newText("Lives: "..lives, 20, 10, native.systemFont, 15); playerLives:setTextColor(0, 119, 112); playerLives.x = 320 --playerLives.y = display.contentHeight / 2 --------------------------------------------------------------------------------- -- --------------------------------------------------------------------------------- --------------------------------------------------------------------------------- -- Add Altazar and Fuel Cell Collisions --------------------------------------------------------------------------------- --Fuel Cell collision -- Detect whether the player is in the air or not function onCollision( event ) -- If guy is touching grass, allow jump if(event.object1.myName == "floor" and event.object2.myName == "dogAnimation") then playerInAir = false end -- Remove coin when it reaches the ground if(event.object1.myName == "floor" and event.object2.myName == "coin") then event.object2:removeSelf() end -- If dog collides with coin, remove coin if(event.object1.myName == "dogAnimation" and event.object2.myName == "coin") then score = score +1 playerScore.text = "Score: " .. score event.object2:removeSelf() audio.play(fuel\_cell) end end Runtime:addEventListener( "collision", onCollision ) --------------------------------------------------------------------------------- -- Add fuel cells --------------------------------------------------------------------------------- --create fuel cell function createFuelCell() coin = display.newImage("images/final/fuelCell-final.png", math.random(20,screenW-20), -25, math.random(8,14), math.random(210,223),7) physics.addBody( coin, "dynamic") coin.myName = "coin" end timer.performWithDelay( 4000, createFuelCell, 0) --1000 equals fast time --------------------------------------------------------------------------------- -- --------------------------------------------------------------------------------- --------------------------------------------------------------------------------- -- Add Calclate Lives --------------------------------------------------------------------------------- function CalcLife() if (lives \> 0) then lives = lives -1 if (lives \> 0) then playerLives.text = "Lives: " .. lives audio.play(life\_lost) else endGame() end else endGame() end end function endGame () storyboard.gotoScene("restart", "fade", 6000) --message = display.newImage("images/GAMEOVER.png") -- message.x = screenW / 2 -- message.y = screenH / 2 -- GameOverBacktoMenu = display.newImage("images/final/menu-levels-button.png") FOR BUTTON FOR MENU -- GameOverBacktoMenu.x = screenW / 2 -- GameOverBacktoMenu.y = 280 --timer.performWithDelay(1200, GameOver, 1) audio.stop(2) --stop audio rain channel=2 end function GameOver() local message1 = display.newText("GAME OVER", 20, 10, native.systemFont, 70) message1:setTextColor( 255, 255, 255 ) message1.x = screenW / 2 message1.y = screenH / 2 audio.play(gameOverScreen) end --function endGame() --check if user is eligible to continue --check if it is not the last level -- storyboard.gotoScene( "menu", "fade", 250 ) -- return true -- indicates successful touch --end --------------------------------------------------------------------------------- -- --------------------------------------------------------------------------------- --------------------------------------------------------------------------------- -- Add Calclate Score --------------------------------------------------------------------------------- --------------------------------------------------------------------------------- -- --------------------------------------------------------------------------------- --------------------------------------------------------------------------------- -- Other Collisions --------------------------------------------------------------------------------- --If dog hits AlienPlant = lose a life function onCollision( event ) -- If dog collides with AlienPlant, lose a life if(event.object1.myName == "AlienPlant" and event.object2.myName == "dogAnimation") then CalcLife() end --If dog collides with cat, = lose a life if(event.object1.myName == "dogAnimation" and event.object2.myName == "catAnimation") then CalcLife() end end Runtime:addEventListener( "collision", onCollision ) --------------------------------------------------------------------------------- -- --------------------------------------------------------------------------------- end --end create scene -- Called immediately after scene has moved onscreen: function scene:enterScene( event ) local group = self.view background1.enterFrame = scrollBackground Runtime:addEventListener("enterFrame", background1) background2.enterFrame = scrollBackground Runtime:addEventListener("enterFrame", background2) landscapeback1.enterFrame = scrollBackground Runtime:addEventListener("enterFrame", landscapeback1) landscapeback2.enterFrame = scrollBackground Runtime:addEventListener("enterFrame", landscapeback2) landscapefront1.enterFrame = scrollBackground Runtime:addEventListener("enterFrame", landscapefront1) landscapefront2.enterFrame = scrollBackground Runtime:addEventListener("enterFrame", landscapefront2) floor1.enterFrame = scrollBackground Runtime:addEventListener("enterFrame", floor1) floor2.enterFrame = scrollBackground Runtime:addEventListener("enterFrame", floor2) --platform1.enterFrame = scrollBackground --Runtime:addEventListener("enterFrame", platform1) --platform2.enterFrame = scrollBackground --Runtime:addEventListener("enterFrame", platform2) end --end scene:enterScene -- Called when scene is about to move offscreen: function scene:exitScene( event ) local group = self.view Runtime:removeEventListener("enterFrame", background1) Runtime:removeEventListener("enterFrame", background2) Runtime:removeEventListener("enterFrame", landscapeback1) Runtime:removeEventListener("enterFrame", landscapeback2) Runtime:removeEventListener("enterFrame", landscapefront1) Runtime:removeEventListener("enterFrame", landscapefront2) Runtime:removeEventListener("enterFrame", floor1) Runtime:removeEventListener("enterFrame", floor2) --Runtime:removeEventListener("enterFrame", platform1) --Runtime:removeEventListener("enterFrame", platform2) storyboard.purgeAll( ) storyboard.removeAll( ) audio.stop(2) --stop audio rain channel=2 end --end scene:exitScene -- Called prior to the removal of scene's "view" (display group) function scene:destroyScene( event ) local group = self.view scene:addEventListener("createScene", scene) scene:addEventListener("enterScene", scene) scene:addEventListener("exitScene", scene) scene:addEventListener("destroyScene", scene) end --scene:destroyScene --------------------------------------------------------------------------------- -- END OF YOUR IMPLEMENTATION --------------------------------------------------------------------------------- -- "createScene" event is dispatched if scene's view does not exist scene:addEventListener( "createScene", 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 ) -- "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 ) --------------------------------------------------------------------------------- return scene

Looks like you have added your game objects into a screenGroup object, but didn’t insert the screenGroup group into the main group scene group .So, you might want to try this:

 local group = self.view --------------------------------------------------------------------------------- --Add Rain --------------------------------------------------------------------------------- screenGroup = display.newGroup() group:insert(screenGroup)

Hi thanks for that, but the minute I put in the code below it put the restart.lua file behind the game.lua and then crashes? 

function endGame () storyboard.gotoScene("restart", "fade", 6000) audio.stop(2) --stop audio rain channel=2 end

Do you still have the below line in your game.lua code?

group = display.newGroup()

Remember, “group” is being used by Storyboard to identify the group display object that is purged when the scene is removed. You could of course change it to “screenGroup” from “group”, but I believe you need to ensure this it’s consistent through your scene.

function scene:createScene( event ) local group = self.view --------------------------------------------------------------------------------- --Add Rain --------------------------------------------------------------------------------- screenGroup = display.newGroup() group:insert(screenGroup)

I have this is and now have a function that goes to restart.lua but need to remove everything, back to restart?

I’m not sure what you’re asking in the above, but a little more information about restarting levels can be found in this blog post:

http://www.coronalabs.com/blog/2013/08/20/tutorial-reloading-storyboard-scenes/

Actual no I dont I thought it was going to restart.lua but its just crashing now 

Can you restate the issue you are encountering now? I’m a bit confused as to the problem you are now running in to.

Sorry for the confusion I have this code now, but is crashing the simluator? 

function CalcLife() if (lives \> 0) then lives = lives -1 if (lives \> 0) then playerLives.text = "Lives: " .. lives audio.play(life\_lost) else endGame() end else endGame() end end function endGame() audio.play(gameOverScreen) storyboard.gotoScene( "menu", "fade", 200 ) end

I’m looking to be able to end the game if the character has no lives left, and go to restart.lua file?

What error are you getting and what line does it say it’s crashing on?

Rob

--\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* -- -- game.lua -- --\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* --\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* --Requires-- --\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* local storyboard = require( "storyboard" ) local scene = storyboard.newScene() local physics = require "physics" physics.start() local widget = require "widget" --\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* --Varibales --\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* local score = 0 -- Set Score to 10 local lives = 3 -- Set Lives to 3 local message = "" local playerInAir = true -- Set a boolean of whether our dog is in the air or not --Add sound local fuel\_cell = audio.loadSound("fuelcell.wav") -- Set sound variable local life\_lost = audio.loadSound("game\_over.wav") -- Set sound variable local gameOverScreen = audio.loadSound("game-over.wav") -- Set sound variable local missionComplete = audio.loadSound("missoncomplete.wav") -- Set sound variable --\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* --BEGINNING OF GAME IMPLEMENTATION --\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* -- Called when the scene's view does not exist: function scene:createScene( event ) local screenGroup = self.view --------------------------------------------------------------------------------- --Add Rain --------------------------------------------------------------------------------- group = display.newGroup() local g = graphics.newGradient( { 100, 100, 100 }, { 100, 100, 100 }, "down" ) --local bg = display.newRect( 0, 0, screenW, screenH) --bg:setFillColor( g ) --bg.alpha = 0.5 --group:insert(bg) local rain = require("rain") rain.new(group, {}) --Pause rain: rain.pause() --Resume rain: rain.resume() --------------------------------------------------------------------------------- --End Rain --------------------------------------------------------------------------------- --------------------------------------------------------------------------------- -- Add Ceiling --------------------------------------------------------------------------------- --local ceiling = display.newRect(0,-5,960,5) --physics.addBody( ceiling, "static") --group:insert( ceiling ) --------------------------------------------------------------------------------- -- --------------------------------------------------------------------------------- --------------------------------------------------------------------------------- --Add Backgrounds --------------------------------------------------------------------------------- local background = display.newImage("images/final/background-1.png") screenGroup:insert(background) background1 = display.newImage("images/final/background-1.png") background1:setReferencePoint(display.BottomLeftReferencePoint) background1.x = 0 background1.y = 320 background1.speed = 0.1 screenGroup:insert(background1) background2 = display.newImage("images/final/background-2.png") background2:setReferencePoint(display.BottomLeftReferencePoint) background2.x = 480 background2.y = 320 background2.speed = 0.1 screenGroup:insert(background2) landscapeback1 = display.newImage("images/final/landscape-back-1.png") landscapeback1:setReferencePoint(display.BottomLeftReferencePoint) landscapeback1.x = 0 landscapeback1.y = 320 landscapeback1.speed = 0.2 screenGroup:insert(landscapeback1) landscapeback2 = display.newImage("images/final/landscape-back-2.png") landscapeback2:setReferencePoint(display.BottomLeftReferencePoint) landscapeback2.x = 480 landscapeback2.y = 320 landscapeback2.speed = 0.2 screenGroup:insert(landscapeback2) landscapefront1 = display.newImage("images/final/landscape-front-1.png") landscapefront1:setReferencePoint(display.BottomLeftReferencePoint) landscapefront1.x = 0 landscapefront1.y = 320 landscapefront1.speed = 0.8 screenGroup:insert(landscapefront1) landscapefront2 = display.newImage("images/final/landscape-front-2.png") landscapefront2:setReferencePoint(display.BottomLeftReferencePoint) landscapefront2.x = 480 landscapefront2.y = 320 landscapefront2.speed = 0.8 screenGroup:insert(landscapefront2) floor1 = display.newImage("images/final/floorLargeBlue.png") floor1:setReferencePoint(display.BottomLeftReferencePoint) floor1.x = 0 floor1.y = 320 floor1.speed = 1.0 physics.addBody( floor1, "static") floor1.myName = "floor" --for collision screenGroup:insert(floor1) floor2 = display.newImage("images/final/floorLargeBlue.png") floor2:setReferencePoint(display.BottomLeftReferencePoint) floor2.x = 480 floor2.y = 320 floor2.speed = 1.0 physics.addBody( floor2, "static") floor2.myName = "floor" --for collision screenGroup:insert(floor2) --platform1 = display.newImage("images/final/platformBlueLarge.png") --platform1:setReferencePoint(display.BottomLeftReferencePoint) --platform1.x = 500 --platform1.y = 200 --platform1.speed = 1.0 --physics.addBody( platform1, "static") --screenGroup:insert(platform1) --platform2 = display.newImage("images/final/platformBlueMed.png") --platform2:setReferencePoint(display.BottomLeftReferencePoint) --platform2.x = 1000 --platform2.y = 150 --platform2.speed = 1.0 --physics.addBody( platform2, "static") --screenGroup:insert(platform2) --------------------------------------------------------------------------------- --Scroll Backgrounds --------------------------------------------------------------------------------- function scrollBackground(self,event) if self.x \< -477 then self.x = 480 else self.x = self.x - self.speed end end --------------------------------------------------------------------------------- --End Backgrounds --------------------------------------------------------------------------------- --------------------------------------------------------------------------------- -- Add Plant Enimies --------------------------------------------------------------------------------- --------------------------------------------------------------------------------- -- Add Alien Plant 1 --------------------------------------------------------------------------------- local function onTimer( event ) local obj = event.source.objectID obj:setLinearVelocity( -20, 0 ) end local AlienPlant = display.newImage( "images/final/alien-plant-large.png" ) AlienPlant.x = 700 --off screen AlienPlant.y = 236 --on floor physics.addBody( AlienPlant, "kinematic" ) AlienPlant:setLinearVelocity( 0, 0 ) AlienPlant.myName = "AlienPlant" --for collision local t = timer.performWithDelay( 250, onTimer ) ; t.objectID = AlienPlant --timer --------------------------------------------------------------------------------- -- Add Alien Plant 2 --------------------------------------------------------------------------------- local function onTimer( event ) local obj = event.source.objectID obj:setLinearVelocity( -20, 0 ) end local AlienPlant = display.newImage( "images/final/alien-plant-large.png" ) AlienPlant.x = 900 --off screen AlienPlant.y = 236 --on floor physics.addBody( AlienPlant, "kinematic" ) --or "dynamic" interact with object AlienPlant:setLinearVelocity( 0, 0 ) AlienPlant.myName = "AlienPlant" --for collision local t = timer.performWithDelay( 250, onTimer ) ; t.objectID = AlienPlant --timer --------------------------------------------------------------------------------- --End Plant Enimies --------------------------------------------------------------------------------- --------------------------------------------------------------------------------- -- Add Platforms --------------------------------------------------------------------------------- --------------------------------------------------------------------------------- -- Add Platforms1 --------------------------------------------------------------------------------- local function onTimer( event ) local obj = event.source.objectID obj:setLinearVelocity( -30, 0 ) --speed end local platform1 = display.newImage( "images/final/platformBlueMed.png" ) platform1.x = 1000 --off screen platform1.y = 200 --from floor physics.addBody( platform1 , "kinematic" ) --or "dynamic" interact with object platform1:setLinearVelocity( 0, 0 ) local t = timer.performWithDelay( 1100, onTimer ) ; t.objectID = platform1 --timer --------------------------------------------------------------------------------- -- Add Platforms2 --------------------------------------------------------------------------------- local function onTimer( event ) local obj = event.source.objectID obj:setLinearVelocity( -30, 0 ) --speed end local platform2 = display.newImage( "images/final/platformBlueMed.png" ) platform2.x = 1500--off screen platform2.y = 100 --from floor physics.addBody( platform2 , "kinematic" ) --or "dynamic" interact with object platform2:setLinearVelocity( 0, 0 ) local t = timer.performWithDelay( 1100, onTimer ) ; t.objectID = platform2 --timer --------------------------------------------------------------------------------- -- End Platforms --------------------------------------------------------------------------------- --------------------------------------------------------------------------------- -- Add Character to Game --------------------------------------------------------------------------------- -- Running Sprite local sheetOptions= { width=70, height=49, numFrames=9, sheetContentWidth=210, sheetContentHeight=147 } myLeftImageSheet = graphics.newImageSheet("images/dog-final-right.png", sheetOptions ) myRightImageSheet = graphics.newImageSheet("images/dog-final-left.png", sheetOptions ) myJumpRightImageSheet = graphics.newImageSheet( "images/dog-final-jump-left.png", sheetOptions) local sequenceData = { { name="leftRightRun", start=1, sheet=myLeftImageSheet, count=9, time = 1100}, --, loopCount = 2 { name="upJumpright", sheet= myJumpRightImageSheet, frames= {1,2,3,4,5,6,7,8,9}, time = 1100}, --, loopCount = 4 { name="rightLeftRun", sheet= myRightImageSheet, frames= {1,2,3,4,5,6,7,8,9}, time = 1100}, --loopCount = 2 } --now build the sprite to hold them and place it on the screen local dogAnimation = display.newSprite( myLeftImageSheet, sequenceData ) dogAnimation.x = screenW / 2 --enter 5 to position slightly of screen --the sprite position horizontally dogAnimation.y = screenH / 2 --the sprite position vertically --add physics physics.addBody( dogAnimation, "dynamic", { density=0.9, friction=0.5, bounce=0 } ) --dogAnimation:setLinearVelocity(300,100)-- change the velocity of the rigid body --dogAnimation:applyForce (100,100,100,100) --physics.setGravity (0.2, 0.2) dogAnimation.isFixedRotation = true dogAnimation.myName = "dogAnimation" --for collision dogAnimation:setLinearVelocity( 20,4 ) --play spritesheet dogAnimation:play() --dogAnimation:pause() --------------------------------------------------------------------------------- -- End Alatazar --------------------------------------------------------------------------------- --------------------------------------------------------------------------------- -- Add Enimies CAT and BIRD to Game --------------------------------------------------------------------------------- --Cat Running Sprite local sheetOptions= { width=100, height=70, numFrames=9, sheetContentWidth=300, sheetContentHeight=210 } --now use the options table to build 2 imageSheets- one for lright running --and the other for rleft running myCatImageSheet = graphics.newImageSheet("images/cat-final-left.png", sheetOptions ) -- add multiple sequences from these different sheets with different frame counts (for fast/slow) local sequenceData = { { name="catRight", start=1, sheet= myLeftImageSheet, count=9, time = 700,}, --, loopCount = 2 } --now build the sprite to hold them and place it on the screen local catAnimation = display.newSprite( myCatImageSheet, sequenceData ) catAnimation.x = 200--the sprite position horizontally catAnimation.y = screenH / 2 --the sprite position vertically --add physics physics.addBody( catAnimation, "dynamic", { density=0.2, friction=0.2, bounce=0.2 } ) --catAnimation:setLinearVelocity(300,100)-- change the velocity of the rigid body --catAnimation:applyForce (100,100,100,100) --physics.setGravity (0.2, 0.2) catAnimation.isFixedRotation = true catAnimation.myName = "catAnimation" --for collision --move cat catAnimation:translate(1000, -200) -- -200 above top of screen 400 left of screen transition.to(catAnimation, {x = -800, time = 10000}) --x = -800 run off screen to left --y = 200 --alpha = 0, catAnimation:play() --catAnimation:pause() --Bird Running Sprite local sheetOptions= { width=100, height=70, numFrames=5, sheetContentWidth=300, sheetContentHeight=140 } --now use the options table to build 2 imageSheets- one for lright running --and the other for rleft running --now use the options table to build 2 imageSheets- one for lright running --and the other for rleft running myBirdImageSheet = graphics.newImageSheet("images/bird-final-right.png", sheetOptions ) -- add multiple sequences from these different sheets with different frame counts (for fast/slow) local sequenceData = { { name="birdFlyingRight", start=1, sheet= myLeftImageSheet, count=5, time = 700}, --, loopCount = 2 { name="birdFlyingRight", sheet= myBirdImageSheet, frames= {1,2,3,4,5}, time = 250}, --, loopCount = 4 --, loopDirection = bounce } --now build the sprite to hold them and place it on the screen local birdAnimation = display.newSprite( myBirdImageSheet, sequenceData ) birdAnimation.x = -200--the sprite position horizontally birdAnimation.y = 160 --the sprite position vertically birdAnimation.rotation = -5 birdAnimation.myName = "birdAnimation" --for collision --move bird birdAnimation:translate(birdAnimation.width/2 , -110) transition.to(birdAnimation, {x = 700, time = 20000}) --y = 200 --alpha = 0, --add physics --physics.addBody( birdAnimation, "dynamic", { density=0.5, friction=0.5, bounce=0.3 } ) --birdAnimation:setLinearVelocity(300,100)-- change the velocity of the rigid body --birdAnimation:applyForce (100,100,100,100) --physics.setGravity (0.2, 0.2) --play spritesheet birdAnimation:play() --birdAnimation:pause() --------------------------------------------------------------------------------- -- --------------------------------------------------------------------------------- --------------------------------------------------------------------------------- -- Add Buttons --------------------------------------------------------------------------------- ---Control Alatazar Button functions local function buttonTouch(event) --take in the event and get the associated display object local buttonObj = event.target --now find out which button it is local nameString = buttonObj.id -- define a variable for the final x coordinate of the transition local finalX = dogAnimation.width/2 -- remember registration point is half way! --define another variable for the sequence to play, default left to right local spriteSequence = "leftRightRun" --now use a conditional to set left/right bounds for the transition --and also the animation sequence to load if nameString == "leftButton" then finalX = dogAnimation.width/2 -- default to left spriteSequence = "rightLeftRun" elseif nameString == "rightButton" then finalX = display.contentWidth - dogAnimation.width/2 -- right hand side of screen spriteSequence = "leftRightRun" elseif nameString == "upButton" then finalX = display.contentWidth - dogAnimation.width/2 -- right hand side of screen spriteSequence = "upJumpright" dogAnimation:applyLinearImpulse(0,-9,-9,0 ) end --with the final x value set (based on the button pressed) --the transition can be called after "began" phase if event.phase == "began" then --call the transition, now with added easing! transition.to(dogAnimation, {x = finalX, transition = easing.inCirc, time = 3000}) --was .outExpo 18000 --also load the relevant animation sequence and play it dogAnimation:setSequence( spriteSequence ) dogAnimation:play() elseif event.phase == "ended" then end --remember the issue with event propagation! return true end ---Control Alatazar Button --left button widget local leftButton = widget.newButton { defaultFile = "images/backDefault.png", overFile = "images/backOver.png", id = "leftButton", --id is unique and essential when using event handlers (see below) width = 64, height = 64, onEvent = buttonTouch, } --rightbutton widget local rightButton = widget.newButton{ id = "rightButton", --id is again unique defaultFile = "images/forwardDefault.png", overFile = "images/forwardOver.png", width = 64, height = 64, onEvent = buttonTouch, } --upbutton widget local upButton = widget.newButton{ id = "upButton", --id is again unique defaultFile = "images/upDefault.png", overFile = "images/upOver.png", width = 64, height = 64, onEvent = buttonTouch, } ---HUD Buttons --pausebutton widget local pauseButton = widget.newButton{ id = "pauseButton", --id is again unique defaultFile = "images/pauseDefault.png", overFile = "images/pauseOver.png", width = 30, height = 30, --onEvent = buttonTouch, } --resetbutton widget local resetButton = widget.newButton{ id = "resetButton", --id is again unique defaultFile = "images/resetDefault.png", overFile = "images/resetOver.png", width = 30, height = 30, --onEvent = buttonTouch, } --Positioning buttons leftButton.y = display.contentHeight - leftButton.height/2 rightButton.x = 120 rightButton.y = display.contentHeight - rightButton.height/2 upButton.x = display.contentWidth - rightButton.width/2 upButton.y = display.contentHeight - rightButton.height/2 --HUD pauseButton.x = screenW-460 pauseButton.y = 20 resetButton.x = 460 resetButton.y = 20 --------------------------------------------------------------------------------- -- --------------------------------------------------------------------------------- --------------------------------------------------------------------------------- -- Add HUD Score and Lives --------------------------------------------------------------------------------- --Add Score to Screen local playerScore = display.newText("Score: "..score, 20, 10, native.systemFont, 15); playerScore:setTextColor(255, 255, 255); playerScore.x = screenW / 2 --playerScore.y = display.contentHeight / 2 --Add lives to Screen local playerLives = display.newText("Lives: "..lives, 20, 10, native.systemFont, 15); playerLives:setTextColor(0, 119, 112); playerLives.x = 320 --playerLives.y = display.contentHeight / 2 --------------------------------------------------------------------------------- -- --------------------------------------------------------------------------------- --------------------------------------------------------------------------------- -- Add Altazar and Fuel Cell Collisions --------------------------------------------------------------------------------- --Fuel Cell collision -- Detect whether the player is in the air or not function onCollision( event ) -- If guy is touching grass, allow jump if(event.object1.myName == "floor" and event.object2.myName == "dogAnimation") then playerInAir = false end -- Remove coin when it reaches the ground if(event.object1.myName == "floor" and event.object2.myName == "coin") then event.object2:removeSelf() end -- If dog collides with coin, remove coin if(event.object1.myName == "dogAnimation" and event.object2.myName == "coin") then CalcScore() event.object2:removeSelf() audio.play(fuel\_cell) end end Runtime:addEventListener( "collision", onCollision ) --------------------------------------------------------------------------------- -- Add fuel cells --------------------------------------------------------------------------------- --create fuel cell function createFuelCell() coin = display.newImage("images/final/fuelCell-final.png", math.random(20,screenW-20), -25, math.random(8,14), math.random(210,223),7) physics.addBody( coin, "dynamic") coin.myName = "coin" end timer.performWithDelay( 1000, createFuelCell, 0) --1000 equals time --------------------------------------------------------------------------------- -- --------------------------------------------------------------------------------- --------------------------------------------------------------------------------- -- Add Calclate Score --------------------------------------------------------------------------------- function CalcScore() if (score \< 3 ) then score = score +1 if (score \< 3 ) then playerScore.text = "Score: " .. score audio.play(fuel\_cell) else nextLevel() end else nextLevel() end end function nextLevel() audio.play(missionComplete) storyboard.gotoScene( "restart", "fade", 200 ) end --------------------------------------------------------------------------------- -- --------------------------------------------------------------------------------- --------------------------------------------------------------------------------- -- Add Calclate Lives --------------------------------------------------------------------------------- function CalcLife() if (lives \> 0) then lives = lives -1 if (lives \> 0) then playerLives.text = "Lives: " .. lives audio.play(life\_lost) else endGame() end else endGame() end end function endGame() audio.play(gameOverScreen) storyboard.gotoScene( "menu", "fade", 200 ) end --------------------------------------------------------------------------------- -- --------------------------------------------------------------------------------- --------------------------------------------------------------------------------- -- Other Collisions --------------------------------------------------------------------------------- --If dog hits AlienPlant = lose a life function onCollision( event ) -- If dog collides with AlienPlant, lose a life if(event.object1.myName == "AlienPlant" and event.object2.myName == "dogAnimation") then CalcLife() end --If dog collides with cat, = lose a life if(event.object1.myName == "dogAnimation" and event.object2.myName == "catAnimation") then CalcLife() end end Runtime:addEventListener( "collision", onCollision ) --------------------------------------------------------------------------------- -- --------------------------------------------------------------------------------- end --end create scene -- Called immediately after scene has moved onscreen: function scene:enterScene( event ) local group = self.view background1.enterFrame = scrollBackground Runtime:addEventListener("enterFrame", background1) background2.enterFrame = scrollBackground Runtime:addEventListener("enterFrame", background2) landscapeback1.enterFrame = scrollBackground Runtime:addEventListener("enterFrame", landscapeback1) landscapeback2.enterFrame = scrollBackground Runtime:addEventListener("enterFrame", landscapeback2) landscapefront1.enterFrame = scrollBackground Runtime:addEventListener("enterFrame", landscapefront1) landscapefront2.enterFrame = scrollBackground Runtime:addEventListener("enterFrame", landscapefront2) floor1.enterFrame = scrollBackground Runtime:addEventListener("enterFrame", floor1) floor2.enterFrame = scrollBackground Runtime:addEventListener("enterFrame", floor2) --platform1.enterFrame = scrollBackground --Runtime:addEventListener("enterFrame", platform1) --platform2.enterFrame = scrollBackground --Runtime:addEventListener("enterFrame", platform2) end --end scene:enterScene -- Called when scene is about to move offscreen: function scene:exitScene( event ) local group = self.view Runtime:removeEventListener("enterFrame", background1) Runtime:removeEventListener("enterFrame", background2) Runtime:removeEventListener("enterFrame", landscapeback1) Runtime:removeEventListener("enterFrame", landscapeback2) Runtime:removeEventListener("enterFrame", landscapefront1) Runtime:removeEventListener("enterFrame", landscapefront2) Runtime:removeEventListener("enterFrame", floor1) Runtime:removeEventListener("enterFrame", floor2) --Runtime:removeEventListener("enterFrame", platform1) --Runtime:removeEventListener("enterFrame", platform2) audio.stop(2) --stop audio rain channel=2 end --end scene:exitScene -- Called prior to the removal of scene's "view" (display group) function scene:destroyScene( event ) local group = self.view scene:addEventListener("createScene", scene) scene:addEventListener("enterScene", scene) scene:addEventListener("exitScene", scene) scene:addEventListener("destroyScene", scene) end --scene:destroyScene --------------------------------------------------------------------------------- -- END OF YOUR IMPLEMENTATION --------------------------------------------------------------------------------- -- "createScene" event is dispatched if scene's view does not exist scene:addEventListener( "createScene", 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 ) -- "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 ) --------------------------------------------------------------------------------- return scene

not getting an error its crashing the simulator?

on two functions calcLive and calcScore

You are adding your collision event handler twice.  Are you stopping physics before you leave the scene? 

Rob