You’re adding the objects to a Lua table, but when you remove the objects in the onTouch event, you’re not removing them from the table. So then, when the clean() function is called, it still thinks there are 5 items in the table. So you could remove that item manually from the table, which is certainly possible but is a bit of a pain (either store a reference to its position in the table, or iterate through the entire table until you find the right object), or you could just convert your table t to a display group:
[lua]local t = display.newGroup()
local i = 0
local fireT = {}
local fireNum = 0
local function fire(x,y)
local img = display.newCircle(0,0,5)
img:setFillColor(255,0,0)
img.x = x
img.y = y
transition.to(img, {time=500, x=500})
return img
end
local function spawn()
local timr
local image = display.newRect(0,0,50,50)
image.x = math.random(10,100)
image.y = math.random(30,300)
transition.to(image, {time=10000, x = 500})
local function add()
fireT[#fireT+1] = fire(image.x,image.y)
end
timr = timer.performWithDelay(150, add, 10)
local function onTouch(event)
if event.phase == “ended” then
–image.isVisible = false
timer.cancel(timr)
image:removeSelf()
end
return true
end
image:addEventListener(“touch”, onTouch)
return image
end
for i=1,5 do
local obj = spawn()
t:insert(obj)
end
local function clean()
for i=t.numChildren, 1,-1 do
t[i]:removeSelf()
end
end
timer.performWithDelay(2000, clean,1)[/lua]
Much cleaner, much easier. This is why I prefer display groups over regular tables for storing references to display objects!
For the fired objects, you could do something similar, I suppose, by storing references to them somewhere. But if you want to remove them on collision, and you’re using the physics engine for collision detection, then you can remove them using the properties in the event object that the collision event handler will receive.
Darren [import]uid: 1294 topic_id: 18286 reply_id: 74038[/import]