Adding a listener to and removing spawned objects

Hi all,

I’m trying to add some collectible coins that spawn at random and are collected by touch. I was able to spawn some random physical objects that could disappear on collision events, but I’m not sure how to go about with touch events. I did see this:

http://www.coronalabs.com/blog/2011/09/14/how-to-spawn-objects-the-right-way/

Should I just add to a table of coin objects as they spawn, and then use a global touch-triggered ID filter that removes coin table element #X from the table and then sets the same coin to nil? Or is there a more efficient way?

Thanks,

Lionel

[import]uid: 144359 topic_id: 29445 reply_id: 329445[/import]

[code]
spawnTrue = true;

coin = {}
i = 1
function getCoins(event)
if event.phase == “ended” then
test = event.target
test:removeSelf()
test = nil
end
end

function spawnStuff( event )

if spawnTrue == true then
coin[i] = display.newCircle( 0, 0, 15 )
coin[i]:setFillColor( 255, 255, 0 )
coin[i].x = math.random( 0, 320 )
coin[i].y = math.random( 0, 320 )
coin[i]:addEventListener( “touch”, getCoins )
i = i + 1
end
end

spawnBalls = timer.performWithDelay( 500, spawnStuff, 0 )
[/code] [import]uid: 77199 topic_id: 29445 reply_id: 118270[/import]

Thanks very much. [import]uid: 144359 topic_id: 29445 reply_id: 118285[/import]

And if I could ask… what would efficient code be for cleaning up these coins as a group when I need to clear the screen for other events? I saw that removing a group isn’t as good as setting objects to nil.

I didn’t have many objects in Director’s localGroup so I was thinking of doing a loop and removing any objects with a label of “coin”.

Thanks.

P.S. I tried kludging this together but I got an error saying allItems wasn’t global.

[code]
local function coinClean()
for pp = 1, #allItems do
local anItem = allItems[pp]
if (anItem.x) then
if anItem.nickname==“coin”
then
anItem:removeSelf()
anItem=nil
end
end
end
end

[code] [import]uid: 144359 topic_id: 29445 reply_id: 118296[/import]

Easy enough, if you want to clean the coins only you just create a group and insert them into a group each time one is created:

spawnTrue = true;  
   
coin = {}  
allCoins = display.newGroup()  
i = 1  
   
   
function getCoins(event)  
 if event.phase == "ended" then   
 test = event.target  
 test:removeSelf()  
 test = nil  
 end  
end  
   
function spawnStuff( event )  
   
 if spawnTrue == true then  
 coin[i] = display.newCircle( 0, 0, 15 )  
 coin[i]:setFillColor( 255, 255, 0 )  
 coin[i].x = math.random( 0, 320 )  
 coin[i].y = math.random( 0, 320 )  
 allCoins:insert(coin[i])  
 coin[i]:addEventListener( "touch", getCoins )  
 i = i + 1  
 end  
 end  
   
spawnBalls = timer.performWithDelay( 500, spawnStuff, 0 )  
  
function killCoins( event )  
 if event.phase == "ended" then  
 allCoins:removeSelf()  
 allCoins = nil  
 allCoins = display.newGroup()  
 end  
end  
  
button = display.newRect( 150, 150, 50, 50)  
button:addEventListener( "touch", killCoins )  

The reason why I gave you such a short killCoins function is to show you that after you kill the entire group you have to recreate it so that you don’t get an error from your spawnCoin function trying to insert the coins into a group that doesn’t exist anymore. So make sure you recreate it after. A timer.pause would probably go in there to, to stop the coins from being created until you are ready again. [import]uid: 77199 topic_id: 29445 reply_id: 118319[/import]