removeSelf() and Collision Detection

Evening all.

Another question from someone just starting out with Corona.
I’ve now got my player and item objects jumping around the screen, and I set up a global collisions listener to see if the player collides with any items and reward them for it.

-- score gui  
local score = display.newText( "0", player.x, player.y-70, "Helvetica", 34)  
  
local function onCollision(event)  
 if (event.object1.name == "item" and event.object2.name == "player") then  
 score.text = score.text + 100  
 item:removeSelf()  
 end  
end  
  
Runtime:addEventListener ("collision", onCollision)  

It all works well and good, however, every so often the collision occurs before the item is removed, and the player smashes directly into it, sending them off in a tangent instead of simply removing the item and allowing them to pass straight through.

Any tips? [import]uid: 81846 topic_id: 14706 reply_id: 314706[/import]

Should it be like this (presuming that obj1 is the item your trying to remove)

[code]
– score gui
local score = display.newText( “0”, player.x, player.y-70, “Helvetica”, 34)

local function onCollision(event)
local obj1 = event.object1
local obj2 = event.object2

if (obj1.name == “item” and obj2.name == “player”) then
score.text = score.text + 100
obj1:removeSelf()
end
end

Runtime:addEventListener (“collision”, onCollision) [import]uid: 84637 topic_id: 14706 reply_id: 54414[/import]

Nope, item is the name of the thing being removed.

It actually does remove it. 100% of the time.

However, sometimes the item is removed, and the player passes through.
But every so often, say 1 in 50 times, the item is removed, but, the player object bounces off the item object for a split second before its removed. [import]uid: 81846 topic_id: 14706 reply_id: 54416[/import]

Could the item be a sensor body ? [import]uid: 84637 topic_id: 14706 reply_id: 54418[/import]

I’m still not sure that you got the question I was asking Danny, but, I looked up the API and read about sensor bodies and that was exactly what I was looking for.

100+ test cases now, and they all worked perfectly.

Cheers Danny. [import]uid: 81846 topic_id: 14706 reply_id: 54419[/import]

Yeah i got the question :slight_smile:

Sorry for not going into more detail in my previous post [import]uid: 84637 topic_id: 14706 reply_id: 54422[/import]