Basic collision handling

local function collide(event)

display.remove (event.object2)

end

Runtime:addEventListener(“collision”, collide)

That’s my basic file.  The display object include circles and rectangles falling in gravity, hitting a “static” ground.  Upon hitting the ground, they disappear.  All fine until I try to separate out the circles from the rectangles and only have circles disappear.  The objects are created in a for loop.  Is this why Corona is showing a “nil” value for their object1.myName?  Why do they disappear upon hitting the ground if they have a “nil” value?

Any help would be greatly appreciated.

Would be useful to see how you’re creating your objects.

The above collision function doesn’t differentiate between objects. You might want to use an object listener instead of a Runtime listener.

 

local testBalloon = {} for i=1, 5 do testBalloon[i] = display.newCircle(96, 96, 124) testBalloon[i].name = ("testBalloon".. i) testBalloon[i].id = i testBalloon[i]:setFillColor(255,0,0) testBalloon[i].x = display.contentWidth/4+(i\*50) testBalloon[i].y = display.contentHeight/2.1+(i\*25) physics.addBody(testBalloon[i],{density=3, bounce=1.00001, shape=balloonShape}) print(testBalloon[i].name) local function testBalloonName(self, event) if event.phase == "ended" then print("Balloon name = "..self.name) self:setFillColor(0,255,0) end end local function onCollision(self, event ) if event.phase == "ended" then print("Balloon id = "..self.id) self:setFillColor(0,0,255) end end testBalloon[i].collision = onCollision testBalloon[i]:addEventListener("collision", testBalloon[i]) testBalloon[i].touch = testBalloonName testBalloon[i]:addEventListener("touch", testBalloon[i]) end

Thanks Alex.  That was really helpful.  I was posting the objects in a for loop but had the listeners outside the loop.  My brain is still wrapping around how the code is compiled.

One question on some of your code… the listeners…

testBalloon[i].collision = onCollision

testBalloon[i]:addEventListener(“collision”, testBalloon[i])

testBalloon[i].touch = testBalloonName

testBalloon[i]:addEventListener(“touch”, testBalloon[i])

How come it’s necessary to declare that testBalloon[i] = onCollision?  How come you cannot simply reference onCollision straight from the addEventListener?

Thanks for your time.

You could do:

testBalloon[i]:addEventListener("touch", testBallonName)

instead, but then you would lose the “self” target. You could always use event.target instead, but I prefer self as I find it more flexible. 

Would be useful to see how you’re creating your objects.

The above collision function doesn’t differentiate between objects. You might want to use an object listener instead of a Runtime listener.

 

local testBalloon = {} for i=1, 5 do testBalloon[i] = display.newCircle(96, 96, 124) testBalloon[i].name = ("testBalloon".. i) testBalloon[i].id = i testBalloon[i]:setFillColor(255,0,0) testBalloon[i].x = display.contentWidth/4+(i\*50) testBalloon[i].y = display.contentHeight/2.1+(i\*25) physics.addBody(testBalloon[i],{density=3, bounce=1.00001, shape=balloonShape}) print(testBalloon[i].name) local function testBalloonName(self, event) if event.phase == "ended" then print("Balloon name = "..self.name) self:setFillColor(0,255,0) end end local function onCollision(self, event ) if event.phase == "ended" then print("Balloon id = "..self.id) self:setFillColor(0,0,255) end end testBalloon[i].collision = onCollision testBalloon[i]:addEventListener("collision", testBalloon[i]) testBalloon[i].touch = testBalloonName testBalloon[i]:addEventListener("touch", testBalloon[i]) end

Thanks Alex.  That was really helpful.  I was posting the objects in a for loop but had the listeners outside the loop.  My brain is still wrapping around how the code is compiled.

One question on some of your code… the listeners…

testBalloon[i].collision = onCollision

testBalloon[i]:addEventListener(“collision”, testBalloon[i])

testBalloon[i].touch = testBalloonName

testBalloon[i]:addEventListener(“touch”, testBalloon[i])

How come it’s necessary to declare that testBalloon[i] = onCollision?  How come you cannot simply reference onCollision straight from the addEventListener?

Thanks for your time.

You could do:

testBalloon[i]:addEventListener("touch", testBallonName)

instead, but then you would lose the “self” target. You could always use event.target instead, but I prefer self as I find it more flexible.