Tap to destroy

Hi everybody!

It’s probably a dumb question, but I will be very thankfully if you can help-me…

I have many objects in screen moving around, and I want to destroy them (with a sprite effect, or not) when I touch them, how can I do it?
Other thing I want to do It’s to get points for every object destroyed.

Thank You. [import]uid: 10586 topic_id: 3522 reply_id: 303522[/import]

You would call removeSelf() from a touch listener:

local img = display.newImage("ball.png")  
local score = 0  
  
function deleteMe(event)  
 local targetObj = event.target  
 targetObj:removeSelf()  
-- pseudo-code for adding points  
 score = score + 10  
end  
  
img:addEventListener("touch", deleteMe)  

Tim
[import]uid: 8196 topic_id: 3522 reply_id: 10611[/import]

Tim I get a memory leak if you loop the function for creating img. How do you remove it completely with a loop?

The code I’m using:

[code]

local x = 100
local y = 4

local score = 0

scoreTextfield = display.newText( " " … score, 200, 105, nil, 40 )
scoreTextfield:setTextColor( 255, 255, 255, 255 )

scoreTextfield.text = " " … score

collectgarbage(“collect”)
score = "System Memory : " … collectgarbage(“count”)
scoreTextfield.text = " " … score

local randomPos = math.random

function deleteMe(event)

local targetObj = event.target
targetObj:removeSelf()

end

local function randomBall ()

local imageBall = display.newRect( randomPos(300), 200, 30, 30 )
imageBall:setFillColor ( 255, 255, 255 )

imageBall:addEventListener(“touch”, deleteMe)

end

local timerBall = timer.performWithDelay( x, randomBall, y )

local function memCheck ()

collectgarbage(“collect”)
score = "System Memory : " … collectgarbage(“count”)
scoreTextfield.text = " " … score

end

local timerCheck = timer.performWithDelay( 10000, memCheck, 1 )

[/code] [import]uid: 10903 topic_id: 3522 reply_id: 12281[/import]