Change to game over screen

is there a way i can do it so as soon as my plane gets hit it goes to the game over screen as of now as i start my game the game over screen is on top of my images

function scene:create( event )

   local sceneGroup = self.view

   

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.

      

      

   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).

      

      – Example: stop timers, stop animation, stop audio, etc.

   elseif ( phase == “did” ) then

      – Called immediately after scene goes off screen.

   end

end

function scene:destroy( event )

  removePlane()

  removeSpike()

   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

local options = {}

effect = “fade”

time = 500

composer.gotoScene(“gameover”)


– Listener setup

scene:addEventListener( “create”, scene )

scene:addEventListener( “show”, scene )

scene:addEventListener( “hide”, scene )

scene:addEventListener( “destroy”, scene )

return scene

First, please post your code using the code formatting option (the blue <> button in the bar with Bold, Italic, Underline, etc.). Either highlight your code and click the button, or click the button and paste your code into the box that pops up (best option).

Next, this block of code:

local options = {} effect = "fade" time = 500 composer.gotoScene("gameover")

Is just sitting in the scene’s main chunk of code. It will be executed when the scene is "require"d in via composer. It will execute before any of your scene’s event functions will get called. Normally this code would be inside a function that is triggered by a button, or a collision event or where you detect the level was won or lost.

Without seeing your code that creates your objects it will be hard to advise more.

Rob

I have been trying to add a collision event, but I fail to understand how it works , how does it work? For example if an object hits my player how could i then add this code

local options = {}

effect = "fade"

time = 500

composer.gotoScene(“gameover”)

local function onCollision( event )

    if event.phase == “began” then

       if event.target.myName == “Plane” and event.other.myName == “spike” then 

        local options = {}

      effect = "fade"

      time = 500

     

 composer.gotoScene(“gameover”)

  display.remove(removeSpike)

  display.remove(removePlane)

 end 

end

end 

Plane:addEventListener(“collision” , onCollision)

why wouldnt this work? The game continues on but does not change scene

Please post your code by clicking on the blue <> button and pasting it in the window that pops up.

Rob

local function onCollision( event ) if event.phase == "began" then if event.target.myName == "Plane" and event.other.myName == "spike" then local options = {} effect = "fade" time = 500 composer.gotoScene("gameover") display.remove(removeSpike) display.remove(removePlane) end end end Plane:addEventListener("collision" , onCollision) 

 

why wouldnt this work? The game continues on but does not change scene

You should put in some print() statements to ensure that the collision is actually happening the way you want it to. Also, you should review the syntax that Composer requires:

https://docs.coronalabs.com/guide/system/composer/index.html#transition-effects

I tried and it did not print anything when the two objects touched 

First, please post your code using the code formatting option (the blue <> button in the bar with Bold, Italic, Underline, etc.). Either highlight your code and click the button, or click the button and paste your code into the box that pops up (best option).

Next, this block of code:

local options = {} effect = "fade" time = 500 composer.gotoScene("gameover")

Is just sitting in the scene’s main chunk of code. It will be executed when the scene is "require"d in via composer. It will execute before any of your scene’s event functions will get called. Normally this code would be inside a function that is triggered by a button, or a collision event or where you detect the level was won or lost.

Without seeing your code that creates your objects it will be hard to advise more.

Rob

I have been trying to add a collision event, but I fail to understand how it works , how does it work? For example if an object hits my player how could i then add this code

local options = {}

effect = "fade"

time = 500

composer.gotoScene(“gameover”)

local function onCollision( event )

    if event.phase == “began” then

       if event.target.myName == “Plane” and event.other.myName == “spike” then 

        local options = {}

      effect = "fade"

      time = 500

     

 composer.gotoScene(“gameover”)

  display.remove(removeSpike)

  display.remove(removePlane)

 end 

end

end 

Plane:addEventListener(“collision” , onCollision)

why wouldnt this work? The game continues on but does not change scene

Please post your code by clicking on the blue <> button and pasting it in the window that pops up.

Rob

local function onCollision( event ) if event.phase == "began" then if event.target.myName == "Plane" and event.other.myName == "spike" then local options = {} effect = "fade" time = 500 composer.gotoScene("gameover") display.remove(removeSpike) display.remove(removePlane) end end end Plane:addEventListener("collision" , onCollision) 

 

why wouldnt this work? The game continues on but does not change scene

You should put in some print() statements to ensure that the collision is actually happening the way you want it to. Also, you should review the syntax that Composer requires:

https://docs.coronalabs.com/guide/system/composer/index.html#transition-effects

I tried and it did not print anything when the two objects touched