Delete an object when it is touched

  1. How can I delete an object when the user touch it.
  2. Is there a way to add scores for every deleted object ?

Thanks in advance … [import]uid: 24939 topic_id: 5386 reply_id: 305386[/import]

[code]

local img = display.newImage( “ball01.png” )
img.x = 100; img.y = 200

local score = 0

function rectlistener()

img:removeSelf()
score = score + 1

end

img:addEventListener(“touch”, rectlistener )

[/code] [import]uid: 10903 topic_id: 5386 reply_id: 18068[/import]

Better yet write the listener like so:

function rectlistener(event)  
 event.target:removeSelf()  
 score = score + 1  
end  

“event.target” refers to the object that was tapped on, without specifying ahead of time what object that is. Now you can use the same function for multiple images.

By the way here is a sample I just put on Code Exchange that is mostly about tables but happens to demo squares that disappear when you tap them:
http://developer.anscamobile.com/code/tables-lua-dictionaries-and-arrays [import]uid: 12108 topic_id: 5386 reply_id: 18206[/import]

Is there a way to get this to work so when I touch the object it deletes from the coords and not the image? I tried this example and its still deleting from the image.
Help, I really need this to work. :confused: [import]uid: 10355 topic_id: 5386 reply_id: 22817[/import]

what are you trying to do? removeSelf removes everything completely. if you want the physics on the object to stop working then make it inactive, or delete it and spawn a new image in it’s place with no body [import]uid: 6645 topic_id: 5386 reply_id: 22847[/import]

Okay so I have a bunch of shapes with defined coordinates for the physics engine. They need to be deleted when touched, I have that working just fine. But they are being deleted from the picture and not the coordinates.

Basically take a triangle, when you go in to touch it, it will remove from the corners of the png. instead of the coords. So you can touch out in space about 2mm and it will still remove…

[import]uid: 10355 topic_id: 5386 reply_id: 22857[/import]

I need it to be removed only when the defined coordinates are touched… [import]uid: 10355 topic_id: 5386 reply_id: 22858[/import]

Can you post the code? [import]uid: 10903 topic_id: 5386 reply_id: 22859[/import]

I have it here!

https://developer.anscamobile.com/forum/2011/02/09/halp-how-do-i-remove-physics-body-touch-event

I cant believe more people have this problem. But im trying my best to explain it. [import]uid: 10355 topic_id: 5386 reply_id: 22861[/import]