Creating listener for each object generated

[lua]apple = {}

for i = noOfApple, 1, -1 do

rarity = mRand(100) --random number from 1 to 100

apple[i] = {}–dynamically create 2D table

if (rarity > 50) then
apple[i].type = red
apple[i] = display.newImageRect(“APPLE_RED.png”, 75, 85)–Red Apple
apple[i].xScale = .5; apple[i].yScale = .5
physics.addBody( apple[i], “dynamic”, {density= 0.01, friction=0.5, bounce=0.15, shape=appleShape } )
apple[i].x = mRand(_W * 0.05,_W * 0.95)–random x postion of apple
apple[i].y = mRand(-(_H * (mRand(1,4) * 0.1)), _H * 0.1)–random y postion of apple

else
apple[i].type = green
apple[i] = display.newImageRect(“APPLE_GREEN.png”, 75, 85)–Green Apple
apple[i].xScale = .5; apple[i].yScale = .5
physics.addBody( apple[i], “dynamic”, {density= 0.01, friction=0.5, bounce=0.15, shape=appleShape } )
apple[i].x = mRand(_W * 0.05,_W * 0.95)–random x postion of apple
apple[i].y = mRand(-(_H * (mRand(1,4) * 0.1)), _H * 0.1)–random y postion of apple
end
end
[lua]the number of apple generated will fall from a random x and y axis but how do i make it disappear by tapping on the apple?
How do i create listener for a table object using object:removeSelf() [import]uid: 40786 topic_id: 8326 reply_id: 308326[/import]

I haven’t tested this but give it a go, first create a function where you first create the apple table like so:

local onTap = function( event )  
 event.target:removeSelf()  
 event.target = nil  
end  

And then when you actually create your apple images add this:

apple[i]:addEventListener( "tap", onTap ) [import]uid: 5833 topic_id: 8326 reply_id: 29755[/import]

up and running thanks alot Graham…!! [import]uid: 40786 topic_id: 8326 reply_id: 29764[/import]

Awesome glad it’s working! [import]uid: 5833 topic_id: 8326 reply_id: 29799[/import]