So i have a ball, box and wall…
when the ball collides with the box then the box disappears
when the ball collides with the wall then the wall disappears
how do i accomplish this? Thanks!
So i have a ball, box and wall…
when the ball collides with the box then the box disappears
when the ball collides with the wall then the wall disappears
how do i accomplish this? Thanks!
Ok so here is some code…
local function onCollision( self, event ) if event.phase == "began" then -- THIS IS LINE 56!!!! \<------------ if (self.myName == "circle" and event.other.myName == "box") then display.remove(box) elseif (self.myName == "circle" and event.other.myName == "wall") then display.remove(wall) end end end box:addEventListener( "collision", onCollision ) wall:addEventListener( "collision", onCollision)
here is the error…
main.lua:56: attempt to index local 'event' (a nil value) -- check above code for line 56
You’re missing the rest of your table listener:
https://docs.coronalabs.com/guide/physics/collisionDetection/index.html#local-collision-handling
what do you mean? looks like i have all of it there…
You have:
box:addEventListener( "collision", onCollision ) wall:addEventListener( "collision", onCollision)
what you want is:
box.collision = onCollision box:addEventListener( "collision", box) wall.collision = onCollision wall:addEventListener( "collision", wall)
That’s just to make sure that the syntax is correct. If I’m reading your function correctly, it looks like the “self” of the collision function has a variable “myName”, which is “circle”. You have added the table listeners to the box and wall objects, which would be the operator “self” in your collision function. If neither of them have the “myName” variable of “circle”, your collision listener won’t work the way you want it to. Take a look through the collision detection guide and make sure you’re assigning the listeners to the objects that you want:
https://docs.coronalabs.com/guide/physics/collisionDetection/index.html#local-collision-handling
Ok so here is some code…
local function onCollision( self, event ) if event.phase == "began" then -- THIS IS LINE 56!!!! \<------------ if (self.myName == "circle" and event.other.myName == "box") then display.remove(box) elseif (self.myName == "circle" and event.other.myName == "wall") then display.remove(wall) end end end box:addEventListener( "collision", onCollision ) wall:addEventListener( "collision", onCollision)
here is the error…
main.lua:56: attempt to index local 'event' (a nil value) -- check above code for line 56
You’re missing the rest of your table listener:
https://docs.coronalabs.com/guide/physics/collisionDetection/index.html#local-collision-handling
what do you mean? looks like i have all of it there…
You have:
box:addEventListener( "collision", onCollision ) wall:addEventListener( "collision", onCollision)
what you want is:
box.collision = onCollision box:addEventListener( "collision", box) wall.collision = onCollision wall:addEventListener( "collision", wall)
That’s just to make sure that the syntax is correct. If I’m reading your function correctly, it looks like the “self” of the collision function has a variable “myName”, which is “circle”. You have added the table listeners to the box and wall objects, which would be the operator “self” in your collision function. If neither of them have the “myName” variable of “circle”, your collision listener won’t work the way you want it to. Take a look through the collision detection guide and make sure you’re assigning the listeners to the objects that you want:
https://docs.coronalabs.com/guide/physics/collisionDetection/index.html#local-collision-handling