Not switching scenes !

I have almost completed my app . I am making the losing screen ( restart.lua ) and I have a collision event that is supposed to take me to it . The problem is when that collision event happens it does not switch scene . However I am not getting any errors that pop up .

Here is my level1.lua code ( collision events on lines 130-156 ) :

local storyboard = require( "storyboard" ) local scene = storyboard.newScene() function scene:createScene( event ) local group = self.view local physics = require( "physics" ) physics.start() physics.setGravity(0, 30) -- display background image local background = display.newImage( "background.png" ) background.x = 10; background.y = 308 -- display ground image local ground = display.newImage( "ground.png" ) ground.x = 145; ground.y = 480 physics.addBody( ground, "static" , { friction=0.5, bounce=0.1 } ) -- create object local myObject = display.newRect( 0, 0, 100, 30 ) myObject:setFillColor( 0 ) myObject.x = 145; myObject.y = 400 physics.addBody( myObject, "kinematic" , { myObject, friction=0.5, bounce=2 } ) -- touch listener function function myObject:touch( event ) if event.phase == "moved" then self.x = event.x self.y = event.y end return true end -- make 'myObject' listen for touch events myObject:addEventListener( "touch", myObject ) local wall = display.newImageRect( "wall.png", 100, 10 ) wall.x, wall.y = -6, 387 wall.rotation = 90 physics.addBody( wall, "static" , { wall, friction=0.5, bounce=0.5 } ) local wall = display.newImageRect( "wall.png", 100, 10 ) wall.x, wall.y = -6, 300 wall.rotation = 90 physics.addBody( wall, "static" , { wall, friction=0.5, bounce=0.5 } ) local wall = display.newImageRect( "wall.png", 100, 10 ) wall.x, wall.y = -6, 202 wall.rotation = 90 physics.addBody( wall, "static" , { wall, friction=0.5, bounce=0.5 } ) local wall = display.newImageRect( "wall.png", 100, 10 ) wall.x, wall.y = -6, 104 wall.rotation = 90 physics.addBody( wall, "static" , { wall, friction=0.5, bounce=0.5 } ) local wall = display.newImageRect( "wall.png", 100, 10 ) wall.x, wall.y = -6, 7 wall.rotation = 90 physics.addBody( wall, "static" , { wall, friction=0.5, bounce=0.5 } ) local wall = display.newImageRect( "wall.png", 100, 10 ) wall.x, wall.y = 50, -49 physics.addBody( wall, "static" , { wall, friction=0.5, bounce=0.5 } ) local wall = display.newImageRect( "wall.png", 100, 10 ) wall.x, wall.y = 135, -49 physics.addBody( wall, "static" , { wall, friction=0.5, bounce=0.5 } ) local wall = display.newImageRect( "wall.png", 100, 10 ) wall.x = 232 wall.y = -49 physics.addBody( wall, "static" , { wall, friction=0.5, bounce=0.5 } ) local wall = display.newImageRect( "wall.png", 100, 10 ) wall.x = 335 wall.y = -49 physics.addBody( wall, "static" , { wall, friction=0.5, bounce=0.5 } ) local wall = display.newImageRect( "wall.png", 100, 10 ) wall.rotation = 90 wall.x = 325 wall.y = 3 physics.addBody( wall, "static" , { wall, friction=0.5, bounce=0.5 } ) local wall = display.newImageRect( "wall.png", 100, 10 ) wall.rotation = 90 wall.x = 325 wall.y = 98 physics.addBody( wall, "static" , { wall, friction=0.5, bounce=0.5 } ) local wall = display.newImageRect( "wall.png", 100, 10 ) wall.x = 325 wall.y = 195 wall.rotation = 90 physics.addBody( wall, "static" , { wall, friction=0.5, bounce=0.5 } ) local wall = display.newImageRect( "wall.png", 100, 10 ) wall.rotation = 90 wall.x = 325 wall.y = 292 physics.addBody( wall, "static" , { wall, friction=0.5, bounce=0.5 } ) local wall = display.newImageRect( "wall.png", 100, 10 ) wall.rotation = 90 wall.x = 325 wall.y = 387 physics.addBody( wall, "static" , { wall, friction=0.5, bounce=0.5 } ) local block = display.newImage( "block.png" ) block.rotation = 8 block.x = 200 physics.addBody( block, "dynamic" , { block, friction=0.5, bounce=0.5 } ) function onCollision(event) if (event.phase=="began") then if (event.object1.myName=="block" and event.object2.myName=="ground") then storyboard.gotoScene("restart", "fade", 500) end elseif (event.phase=="ended") then if (event.object1.myName=="block" and event.object2.myName=="ground") then end end end local block2 = display.newImage( "block2.png" ) block2.rotation = 8 block2.x = 100 physics.addBody( block2, "dynamic" , { block2, friction=0.5, bounce=0.5 } ) function onCollision(event) if (event.phase=="began") then if (event.object1.myName=="block2" and event.object2.myName=="ground") then storyboard.gotoScene("restart", "fade", 500) end elseif (event.phase=="ended") then if (event.object1.myName=="block2" and event.object2.myName=="ground") then end end end group:insert(background) group:insert(ground) group:insert(myObject) group:insert(wall) end function scene:enterScene( event ) local group = self.view end function scene:exitScene( event ) local group = self,view end function scene:destroyScene( event ) local group = self.view end scene:addEventListener( "createScene", scene ) scene:addEventListener( "enterScene", scene ) scene:addEventListener( "exitScene", scene ) scene:addEventListener( "destroyScene", scene ) return scene

here is my restart.lua code( I have only a little cause I am doing a test run so please don’t menschen it  ):

local storyboard = require( "storyboard" ) local scene = storyboard.newScene() function scene:createScene( event ) local group = self.view -- display background image local background = display.newImage( "background.png" ) background.x = 10; background.y = 308 -- display ground image local ground = display.newImage( "ground.png" ) ground.x = 145; ground.y = 480 local loseTxt = display.newImageRect( "loseTxt.png", 300, 200 ) loseTxt.x = 160 loseTxt.y = 20 group:insert( background ) group:insert( ground ) group:insert( loseTxt ) end function scene:exitScene( event ) local group = self,view end function scene:destroyScene( event ) local group = self.view end scene:addEventListener( "createScene", scene ) scene:addEventListener( "enterScene", scene ) scene:addEventListener( "exitScene", scene ) scene:addEventListener( "destroyScene", scene ) return scene 

Why am I getting this problem ? What do I do to fix it ?

Thank you for your help.

You’ve created two onCollision() functions, but you haven’t actually added them as collision listeners, so the functions are never being called.  See here for details on how to add your functions as collision listeners: https://docs.coronalabs.com/daily/guide/physics/collisionDetection/index.html#collisionevents.

  • Andrew

Thank you for your help Andrew.

You’ve created two onCollision() functions, but you haven’t actually added them as collision listeners, so the functions are never being called.  See here for details on how to add your functions as collision listeners: https://docs.coronalabs.com/daily/guide/physics/collisionDetection/index.html#collisionevents.

  • Andrew

Thank you for your help Andrew.