Best way to remove an object on collision

I was wondering if there is a way to use one function that removes an object on a collission. Currently I have a different function for each object that needs to be removed. [import]uid: 11465 topic_id: 6582 reply_id: 306582[/import]

sure. assign them all to the same listener [import]uid: 6645 topic_id: 6582 reply_id: 22846[/import]

Yes, but how would you remove just the object that was hit. [import]uid: 11465 topic_id: 6582 reply_id: 22852[/import]

use event.target

[import]uid: 6645 topic_id: 6582 reply_id: 22856[/import]

Thanks worked perfectly. [import]uid: 12041 topic_id: 6582 reply_id: 22868[/import]

Hello, sorry I’ve just been looking for the same thing and found this old post.

Can you explain how to use event.target a bit further? I’ve looked at the API and can’t figure it out. I currently have 2 objects on the screen that need to be removed on collision.

This is the code I have so far.

  
function onCollideobject1(event)  
 target:removeSelf();  
end  
  
function onCollideobject2(event)  
 target:removeSelf();  
end  
  
function init()  
 target = display.newImage('target.png')  
 target.x = 50;target.y= 50  
 physics.addBody(object1,{ density=3.0, friction=0.5, bounce=0.05, radius=30})  
 object1.bodyType = 'static'  
 object1:addEventListener('collision',onCollideobject1)  
  
 physics.addBody(object2,{ density=3.0, friction=0.5, bounce=0.05, radius=30})  
 object2.bodyType = 'static'  
 object2:addEventListener('collision',onCollideobject2)  
end  

Thanks in advance!
S [import]uid: 45932 topic_id: 6582 reply_id: 33232[/import]

Try this:

[lua]function onCollideObject(event)
event.target:removeSelf()
end

function init()
[…]
object1:addEventListener(‘collision’,onCollideObject)
object2:addEventListener(‘collision’,onCollideObject)
end[/lua] [import]uid: 51516 topic_id: 6582 reply_id: 33237[/import]

You sir are a legend. Thank you very much.

[import]uid: 45932 topic_id: 6582 reply_id: 33242[/import]