Problem deleting objects from a table

Hello, I need help saving a problem …
it goes like this, I have a table of objects , now the objects will be erased if any of this two conditions happen:

1.Touch the object
2. A certain of time has passed

The idea is to touch all the objects in the screen in a certain time limit. when the time limit is reached all the objects will be erased and you wont get any points.

but I have problems doing this because if i don’t manage to get all the objects in that certain of time , sometimes some objects wont disappear. My guess is because I am managing the objects with a table when i touch an object the table number of objects between it decreases by one which changes the index of the objects that are “above” the object i just pressed.

Also a problem with sprites, what is the best way to make a sprite finish its animation before getting deleted?
cause if i do something like
sprite:play()
sprite:removeSelf()
It would instantly disappear. [import]uid: 127101 topic_id: 28619 reply_id: 328619[/import]

Well, I would have to see how you are trying to make those objects disappear after the time limit to tell you where the problem is. Below would be one way to clear out a table if display objects, even if some were removed prior.
[lua]while table.getn(myTable) > 0 do
myTable[1]:removeSelf();
table.remove(myTable);
end[/lua] [import]uid: 147305 topic_id: 28619 reply_id: 115401[/import]

In regards to deleting a table of objects, remember that removing from the lowest index will make all the other elements in the table shift over. For instance, if you do this:

-- wrong way for i = 1, #object do display.remove( object[i] ) object[i] = nil endThe above example will delete object 1, to which objects 2 through 10 will be now 1 through 9. As the loop continues, it will skip object 1 (which is really object 2), delete object 2 (which really is object 3), and so on.

So, the best way of deleting indexed items is by doing it in reverse, or always deleting the first element.-- right way #1 for i = 1, #object do display.remove( object[1] ) object[1] = nil endor-- right way #2 for i = #object, 1, -1 do display.remove( object[i] ) object[i] = nil end

[import]uid: 6084 topic_id: 28619 reply_id: 115403[/import]

Ok, so i changed to what you suggested

[code]
function aja()
if #topos ~= 0 then
tactuales = tactuales - #topos
for i = #topos, 1, -1 do
if not (topos[i] == nil) then
topos[i]:removeEventListener(‘touch’,esconder)
topos[i].agujero.band = false
topos[i]:removeSelf()
topos[i] = nil
end
end
end
end

function esconder(e)
local topo = e.target
if(e.phase == “began”) then
if not (topo == nil) then
topo:removeEventListener(‘touch’,esconder)
topo.tabla[topo.index] = nil
topo:removeSelf()
end
end
return true
end[/code]
the function aja is the one that is called after the time limit has been reached, the function esconder is called when an object is pressed but still after the changed some moles still dont get deleted. [import]uid: 127101 topic_id: 28619 reply_id: 115441[/import]

so i been changing some stuff in my code but i still cant find whats the problem. Can anyone give me another idea on how to do this? [import]uid: 127101 topic_id: 28619 reply_id: 115591[/import]

I do this to delete all the objects in a table:

[lua]while #topos > 0 do
topos[1]:removeSelf()
end
topos = nil
topos = {}[/lua]

I usually do this inside a group object so that I don’t create additional variables:

[lua]while myGroup.numChildren > 0 do
myGroup[1]:removeSelf()
end[/lua]

For sprites, listen to the “sprite” event and delete the sprite on the “ended” phase, then do a removeSelf() and nil the event.target from there:

[lua]local function destroySprite(event)
if event.phase == “ended” then
event.target:removeSelf()
event.target = nil
end
end

mySprite:addEventListener(“sprite”, destroySprite)
mySprite:play()[/lua] [import]uid: 144908 topic_id: 28619 reply_id: 115624[/import]