Collision listener not working .

I just previously had an error but with the help of @schizoid2k and @hasty it is fixed but something else is wrong . My collision listeners ( lines 106- 116 ) aren’t working  . Why is this happening and how do I fix it .

Level1.lua:

local composer = require( "composer" ) local scene = composer.newScene() local physics = require( "physics" ) physics.start() physics.setGravity(0, 200) local score = require( "score" ) local scoreText = score.init({ fontSize = 50, font = "00\_starmap - Shortcut", x = display.contentCenterX, y = 50, maxDigits = 7, leadingZeros = false, filename = "restart.lua", }) local myObject local wall local wall2 local wall3 local block local block2 local background local ground local mAbs = math.abs local mRand = math.random local mDeg = math.deg local mRad = math.rad local mCos = math.cos local mSin = math.sin local mAcos = math.acos local mAsin = math.asin local mSqrt = math.sqrt local mCeil = math.ceil local mFloor = math.floor local mAtan2 = math.atan2 local mPi = math.pi local getInfo = system.getInfo local getTimer = system.getTimer local strMatch = string.match local strFormat = string.format local pairs = pairs function scene:create( event ) local sceneGroup = self.view physics.start() physics.pause() background = display.newImage( "background.png" ) sceneGroup:insert( background ) ground = display.newImage( "ground.png" ) sceneGroup:insert( ground ) ground.isGround = true physics.addBody( ground, "static" , { friction=0.5, bounce=0.1 } ) myObject = display.newRect( 0, 0, 100, 30 ) sceneGroup:insert( myObject ) myObject.isMyObject = true myObject:setFillColor( 0 ) 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 ) sceneGroup:insert( wall ) wall.rotation = 90 physics.addBody( wall, "static" , { wall, friction=0.5, bounce=0.5 } ) wall2 = display.newImageRect( "wall2.png", 500, 300 ) sceneGroup:insert( wall2 ) wall.rotation = 90 physics.addBody( wall2, "static" , { wall2, friction=0.5, bounce=0.5 } ) wall3 = display.newImageRect( "wall3.png", 600, 300 ) sceneGroup:insert( wall3 ) wall3.rotation = 90 physics.addBody( wall3, "static" , { wall3, friction=0.5, bounce=0.5 } ) block = display.newImage( "block.png" ) sceneGroup:insert( block ) block.isBlock = true block.rotation = 8 physics.addBody( block, "dynamic" , { block, friction=0.5, bounce=.8 } ) block.collision = onCollision end onCollision = function( 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 onCollision = function( self, event ) local other = event.other if event.phase == "began" and event.target == block and event.other == myObject then timer.performWithDelay( 30, function ( increaseScore ) score.add(1) end ) self:removeEventListener( "collision" ) end return true end function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then background.x = 10 background.y = 308 ground.x = 145 ground.y = 480 myObject.x = 145 myObject.y = 400 wall.x = -150 wall.y = 250 wall2.x = 210 wall2.y = -194 wall3.x = 470 wall3.y = 250 block.x = 200 block.y = 35 elseif ( phase == "did" ) then physics.start() block:addEventListener( "collision",block ) myObject:addEventListener( "collision",myObject ) -- 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 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

Thank you .

it looks like it should work. You’ve defined the onCollision function twice, so maybe this is why you don’t get what you expect. The one you define second replaces the first, so you’ll only get a score that increments when block hits myObject, and not when block hits ground.

How can i change it so that the add score and goto scene fits in the oncollision thing ? I didnt think it was possible to do this but since you’ve told me otherwise i realized i was terribly wrong.

Thank you

You overwrite your function at 106 with the one at 118.

It also helps if you tell us what you expect it to do and what’s actually happening instead of “its not working”.

Rob

How do I overwrite it do I put the functions next to one another .

Thank you

On line 106 you assign an anoymous function to a variable “onCollision”, i.e.

onCollision = function()

end

That function is performing some actions including the possibility of going to a new scene. Then on line 118, you create a different anonymous function that increases the score. The variable “onCollision” can only hold one of them in which case it’s the last one you added.

Rob

it looks like it should work. You’ve defined the onCollision function twice, so maybe this is why you don’t get what you expect. The one you define second replaces the first, so you’ll only get a score that increments when block hits myObject, and not when block hits ground.

How can i change it so that the add score and goto scene fits in the oncollision thing ? I didnt think it was possible to do this but since you’ve told me otherwise i realized i was terribly wrong.

Thank you

You overwrite your function at 106 with the one at 118.

It also helps if you tell us what you expect it to do and what’s actually happening instead of “its not working”.

Rob

How do I overwrite it do I put the functions next to one another .

Thank you

On line 106 you assign an anoymous function to a variable “onCollision”, i.e.

onCollision = function()

end

That function is performing some actions including the possibility of going to a new scene. Then on line 118, you create a different anonymous function that increases the score. The variable “onCollision” can only hold one of them in which case it’s the last one you added.

Rob