Why are the collision listeners not working ?

I have just properly indented my code so people wont have a hard time answering me .But back to my problem .

I have this error that bugging me and I cant figure it out so I came here . I previously tried a correction that just produced another error ( ill talk about that later in this post ) . This is the error:

Windows simulator build date: May 27 2015 @ 18:15:50

Copyright © 2009-2015  C o r o n a   L a b s   I n c .
        Version: 3.0.0
        Build: 2015.2646
Platform: SM-G900S / x64 / 6.2 / Intel® HD Graphics / 4.0.0 - Build 10.18.10.3
408 / 2015.2646
Loading project from:   c:\users\true\documents\corona projects\bouncy blocks
Project sandbox folder: C:\Users\True\AppData\Roaming\Corona Labs\Corona Simulat
or\Sandbox\bouncy blocks-C116AB3B868BE13E671AF11DFB4D395F\Documents
WARNING: display.setStatusBarMode() not supported in the simulator for SM-G900S
device
Runtime error
c:\users\true\documents\corona projects\bouncy blocks\level1.lua:116: attempt to
 index global ‘sceneGroup’ (a nil value)
stack traceback:
        [C]: in function ‘error’
        ?: in function ‘gotoScene’
        c:\users\true\documents\corona projects\bouncy blocks\menu.lua:25: in fu
nction <c:\users\true\documents\corona projects\bouncy blocks\menu.lua:23>
        ?: in function <?:221>

Level1.lua code:

local composer = require( "composer" ) local scene = composer.newScene() local physics = require( "physics" ) physics.start() physics.setGravity(0, 40) local disposeGroup local onBack function scene:create( event ) local sceneGroup = self.view end function scene:willEnter( event ) local sceneGroup = self.view disposeGroup = display.newGroup() sceneGroup:insert( disposeGroup ) background = display.newImage( "background.png" ) background.x = 10; background.y = 308 ground = display.newImage( "ground.png" ) ground.x = 145; ground.y = 480 ground.myName = "ground" ground.isGround = true physics.addBody( ground, "static" , { friction=0.5, bounce=0.1 } ) myObject = display.newRect( 0, 0, 100, 30 ) myObject.x = 145; myObject.y = 400 myObject:setFillColor( 0 ) myObject.isMyObject = true physics.addBody( myObject, "kinematic" , { myObject, friction=0.5, bounce=2 } ) function myObject:touch( event ) if event.phase == "moved" then self.x = event.x self.y = event.y end return true end myObject:addEventListener( "touch", myObject ) wall = display.newImageRect( "wall.png", 600, 300 ) wall.x = -150 wall.y = 250 wall.rotation = 90 physics.addBody( wall, "static" , { wall, friction=0.5, bounce=0.5 } ) wall2 = display.newImageRect( "wall2.png", 500, 300 ) wall2.x = 210 wall2.y = -194 wall.rotation = 90 physics.addBody( wall2, "static" , { wall2, friction=0.5, bounce=0.5 } ) wall3 = display.newImageRect( "wall3.png", 600, 300 ) wall3.x = 470 wall3.y = 250 wall3.rotation = 90 physics.addBody( wall3, "static" , { wall3, friction=0.5, bounce=0.5 } ) block = display.newImage( "block.png" ) block.x = 200 block.rotation = 8 block.myName = "block" block.isBlock = true physics.addBody( block, "dynamic" , { block, friction=0.5, bounce=0.5 } ) local function onCollision( self, event ) local other = event.other if( event.phase == "began" and self.isBlock and other.isGround ) then timer.performWithDelay( 30, function() composer.gotoScene ( "restart", "fade", 0 ); end) self:removeEventListener( "collision" ) end return true end block.collision = onCollision block:addEventListener( "collision" ) block2 = display.newImage( "block2.png" ) block2.x = 100 block2.rotation = 8 block2.isBlock2 = true physics.addBody( block2, "dynamic" , { block2, friction=0.5, bounce=0.5 } ) local function onCollision( self, event ) local other = event.other if( event.phase == "began" and self.isBlock2 and other.isGround ) then timer.performWithDelay( 30, function() composer.gotoScene ( "restart", "fade", 0 ); end) self:removeEventListener( "collision" ) end return true end block2.collision = onCollision block2:addEventListener( "collision" ) end sceneGroup:insert(background) sceneGroup:insert(ground) sceneGroup:insert(wall) sceneGroup:insert(wall2) sceneGroup:insert(wall3) sceneGroup:insert(block) sceneGroup:insert(block2) sceneGroup:insert(myObject) function scene:didEnter( event ) local sceneGroup = self.view end function scene:willExit( event ) local sceneGroup = self.view end function scene:didExit( event ) local sceneGroup = self.view display.remove( disposeGroup ) disposeGroup = nil end function scene:show( event ) sceneGroup = self.view local willDid = event.phase if( willDid == "will" ) then self:willEnter( event ) elseif( willDid == "did" ) then self:didEnter( event ) end end function scene:hide( event ) sceneGroup = self.view local willDid = event.phase if( willDid == "will" ) then self:willExit( event ) elseif( willDid == "did" ) then self:didExit( event ) end end scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) --------------------------------------------------------------------------------- return scene

Now, the correction that I made was that I took away:

 sceneGroup:insert(background) sceneGroup:insert(ground) sceneGroup:insert(wall) sceneGroup:insert(wall2) sceneGroup:insert(wall3) sceneGroup:insert(block) sceneGroup:insert(block2) sceneGroup:insert(myObject)

But that just made it so that my collision listeners that were supposed to take me to restart.lua didn’t work so I put that block of code back in place and came here .

What is the error in my code and how do I fix it .

P.s ignore the title I put that on mistake .

Thank you for your time kind gent.

First of all, the code is not properly indented. 

 local function onCollision( self, event ) local other = event.other if( event.phase == "began" and self.isBlock2 and other.isGround ) then timer.performWithDelay( 30, function() composer.gotoScene ( "restart", "fade", 0 ); end) self:removeEventListener( "collision" ) end return true end block2.collision = onCollision block2:addEventListener( "collision" ) end sceneGroup:insert(background) sceneGroup:insert(ground) sceneGroup:insert(wall) sceneGroup:insert(wall2) sceneGroup:insert(wall3) sceneGroup:insert(block) sceneGroup:insert(block2) sceneGroup:insert(myObject)

probably should be:

 local function onCollision( self, event ) local other = event.other if( event.phase == "began" and self.isBlock2 and other.isGround ) then timer.performWithDelay( 30, function() composer.gotoScene ( "restart", "fade", 0 ); end) self:removeEventListener( "collision" ) end return true end block2.collision = onCollision block2:addEventListener( "collision" ) end sceneGroup:insert(background) sceneGroup:insert(ground) sceneGroup:insert(wall) sceneGroup:insert(wall2) sceneGroup:insert(wall3) sceneGroup:insert(block) sceneGroup:insert(block2) sceneGroup:insert(myObject)

Your ends should line up with the “function”, “if”, “else”, “elseif”, “for” or “while” that the end goes with. Your code has plenty of cases where this doesn’t happen.  Next it appears you may have function nested inside of other functions. It’s critical that they functions don’t start at the same column. We need to see where functions are inside other functions. If you look at your code, all of your functions start in column1 as far left as it can go yet the ends are indented.   Unless I expend extreme energy to try and reformat this code as I read it, it’s going to be really hard to spot scope problems, which this error appears to be.

Now I think your problem is these lines:

sceneGroup:insert(background) &nbsp;&nbsp;&nbsp;&nbsp;sceneGroup:insert(ground) &nbsp;&nbsp;&nbsp;&nbsp;sceneGroup:insert(wall) &nbsp;&nbsp;&nbsp;&nbsp;sceneGroup:insert(wall2) &nbsp;&nbsp;&nbsp;&nbsp;sceneGroup:insert(wall3) &nbsp;&nbsp;&nbsp;&nbsp;sceneGroup:insert(block) &nbsp;&nbsp;&nbsp;&nbsp;sceneGroup:insert(block2) &nbsp;&nbsp;&nbsp;&nbsp; sceneGroup:insert(myObject)

are likely not part of any function and may be sitting in the module’s main chunk, at which point “sceneGroup” is undefined.

Rob

Just a quick note here: Why is there two onCollision functions? you only need one… just use if and elseif…

And you do need to clean up your code quite a bit… indentations are not all corrects like Rob said…

I will make the indentation corrections you told me but how do I make those lines part of a function .I searched it and found nothing .

They probably should be inside the scene’s scene:create function.  The best way to do this is to insert it immediately after you create it instead of waiting and doing all the inserts at once.  If this cases stacking errors, just make sure you create the objects in the right order.

local background = display.newImageRect("background.png", 570, 360) sceneGroup:insert( background )

First of all, the code is not properly indented. 

 local function onCollision( self, event ) local other = event.other if( event.phase == "began" and self.isBlock2 and other.isGround ) then timer.performWithDelay( 30, function() composer.gotoScene ( "restart", "fade", 0 ); end) self:removeEventListener( "collision" ) end return true end block2.collision = onCollision block2:addEventListener( "collision" ) end sceneGroup:insert(background) sceneGroup:insert(ground) sceneGroup:insert(wall) sceneGroup:insert(wall2) sceneGroup:insert(wall3) sceneGroup:insert(block) sceneGroup:insert(block2) sceneGroup:insert(myObject)

probably should be:

 local function onCollision( self, event ) local other = event.other if( event.phase == "began" and self.isBlock2 and other.isGround ) then timer.performWithDelay( 30, function() composer.gotoScene ( "restart", "fade", 0 ); end) self:removeEventListener( "collision" ) end return true end block2.collision = onCollision block2:addEventListener( "collision" ) end sceneGroup:insert(background) sceneGroup:insert(ground) sceneGroup:insert(wall) sceneGroup:insert(wall2) sceneGroup:insert(wall3) sceneGroup:insert(block) sceneGroup:insert(block2) sceneGroup:insert(myObject)

Your ends should line up with the “function”, “if”, “else”, “elseif”, “for” or “while” that the end goes with. Your code has plenty of cases where this doesn’t happen.  Next it appears you may have function nested inside of other functions. It’s critical that they functions don’t start at the same column. We need to see where functions are inside other functions. If you look at your code, all of your functions start in column1 as far left as it can go yet the ends are indented.   Unless I expend extreme energy to try and reformat this code as I read it, it’s going to be really hard to spot scope problems, which this error appears to be.

Now I think your problem is these lines:

sceneGroup:insert(background) &nbsp;&nbsp;&nbsp;&nbsp;sceneGroup:insert(ground) &nbsp;&nbsp;&nbsp;&nbsp;sceneGroup:insert(wall) &nbsp;&nbsp;&nbsp;&nbsp;sceneGroup:insert(wall2) &nbsp;&nbsp;&nbsp;&nbsp;sceneGroup:insert(wall3) &nbsp;&nbsp;&nbsp;&nbsp;sceneGroup:insert(block) &nbsp;&nbsp;&nbsp;&nbsp;sceneGroup:insert(block2) &nbsp;&nbsp;&nbsp;&nbsp; sceneGroup:insert(myObject)

are likely not part of any function and may be sitting in the module’s main chunk, at which point “sceneGroup” is undefined.

Rob

Just a quick note here: Why is there two onCollision functions? you only need one… just use if and elseif…

And you do need to clean up your code quite a bit… indentations are not all corrects like Rob said…

I will make the indentation corrections you told me but how do I make those lines part of a function .I searched it and found nothing .

They probably should be inside the scene’s scene:create function.  The best way to do this is to insert it immediately after you create it instead of waiting and doing all the inserts at once.  If this cases stacking errors, just make sure you create the objects in the right order.

local background = display.newImageRect("background.png", 570, 360) sceneGroup:insert( background )