Remove properly

Hi,
can anyone tell me how to remove something properly from table?
say i have this:

[lua]local t = {img= “image.png”, color = “blue”, attached = “image2.png”}
local t2 = {}

for i=1,3 do
t2[i] = display.newImage(t[1].img)
t2[i].color = t[1].color
t2[i].attach = t[1].attached
t2[i].attach.part = display.newRect(0,0,50,50)
[lua]and i need to delete it from screen properly, how can i do this? it seems complicated

any help is appreciated [import]uid: 16142 topic_id: 18286 reply_id: 318286[/import]

I think you just need to remove the images and the rectangles. Color and attached are just properties of the image so should go out when you nil the images.

Something like

[code]
for i=1,3 do

display.remove (t2[i].attach.part)
display.remove(t2[i])
t2[i] = nil

end

[/code] [import]uid: 10903 topic_id: 18286 reply_id: 70064[/import]

thanks, thats exactly what i needed [import]uid: 16142 topic_id: 18286 reply_id: 70094[/import]

You can also use table.remove() function. Here’s the syntax:

table.remove( table, position )[/code]In the above, position indicates the index you want removed from the table. [import]uid: 52430 topic_id: 18286 reply_id: 70113[/import]

hey again)
i have another question:

i spawning objects in table like that:
[lua]local function add()
i=i+1
t[i] = spawnNewObj()
end

timer.perfrormWithDelay(1000, add, 10)[/lua]

and the problem is: objects gets removed by “obj:removeSelf” upon touch or collision and its works ok, but if game ended by final collision and there’s some objects still on screen in that table, how can i delete them from screen and from table?

i tried everything i know, still no luck(

any help is appreciated [import]uid: 16142 topic_id: 18286 reply_id: 72203[/import]

You need to keep removing objects from t2 until its length is equal to 0. So, something like:

[lua]while #t2 > 0 do
– remove stuff
end[/lua]

One thing to consider: Using tables to keep track of display objects like this will work, but using Corona display groups to keep similar objects together (and then be able to remove them all easily) will simplify your code somewhat. Here’s an example:

[lua]-- group to hold everything
local myGroup = display.newGroup()

– create some objects
for i = 1, 10 do
local rect = display.newRect( myGroup, math.random(100), math.random(100), 10, 10 )
rect:setFillColor(math.random(255), math.random(255), math.random(255), math.random(255))
end

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

Basically, you’re treating the display group as a table, so if you need to do other things (e.g. update the positions of all objects), you can just iterate through the child objects in the display group.

Thanks,
Darren [import]uid: 1294 topic_id: 18286 reply_id: 72965[/import]

thanks for example, it will come in handy next time) [import]uid: 16142 topic_id: 18286 reply_id: 72969[/import]

Hello again, im still struggle with all that “remove” thing

ok, here’s a question:

i have a spawn function that do this:

[lua]local t = {}
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 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

timer.performWithDelay(150, add, 10)

local function onTouch(event)
if event.phase == “ended” then
image:removeSelf()
end
return true
end

image:addEventListener(“touch”, onTouch)

return image
end

for i=1,5 do
t[i] = spawn()
end[/lua]
question is: how to remove all objects from screen with maximum efficiency(images and\or fires) ? Especially when i tap some objects and they remove themselves

any help is appreciated, this is such a headache for me [import]uid: 16142 topic_id: 18286 reply_id: 74020[/import]

For the ‘fire’ images, probably the easiest thing to do would be to add an onComplete callback that removes the image when it reaches the end of the transition:

[lua]local function fire(x,y)
local img = display.newCircle(0,0,5)
img:setFillColor(255,0,0)
img.x = x
img.y = y
local function onComplete(event)
print(“removing”, img)
img:removeSelf()
end
transition.to(img, {time=500, x=500, onComplete=onComplete})

return img
end[/lua]

You could do the same thing with the other objects if you want to get rid of them automatically at the end of their transition. If you add that, then you may have to cancel the transition before removing the object in the touch event, since having the onComplete callback may prevent the object from getting cleaned up completely.

However, if you want to just remove the objects at any arbitrary point, then you should just iterate through their parent display group. Since you’re not specifying any particular display group, you’d iterate through the stage, which is the “root” display group:

[lua]local stage = display.getCurrentStage()
while stage.numChildren > 0 do
local object = stage[1]
– do any other necessary cleanup/listener removal
object:removeSelf()
end[/lua]
[import]uid: 1294 topic_id: 18286 reply_id: 74027[/import]

thanks for fast response)

but what about this? :
[lua]local t = {}
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
t[i] = spawn()
end
local function clean()
for i=#t, 1,-1 do
t[i]:removeSelf()
end
end

timer.performWithDelay(2000, clean,1)[/lua]

clean function throws me an “removeSelf” error if i removed even one object from screen before it calls, if there’s a way to properly iterate through table and remove all objects?
and what about fires? what if i remove them like that upon collision?
by the way, i discovered this function :
[lua]local function one()
for i=#t, 1,-1 do
display.remove(t[i])
t[i] = nil

print(t[i])
end
end

timer.performWithDelay(2000, one,1)[/lua]

and it works without any errors, whats so different about it and should i use it, is it safe for memory management? [import]uid: 16142 topic_id: 18286 reply_id: 74030[/import]

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]

display.remove is just a convenience function, I believe, that checks to make sure the object can be removed before trying to remove it. I think it equates to:

[lua]if obj.removeSelf then
obj:removeSelf()
obj = nil
end[/lua]

So probably safer to use if you’re not sure that the object will definitely exist when you try to remove it. [import]uid: 1294 topic_id: 18286 reply_id: 74040[/import]

so, advice for future: use the display groups for storing and removing display objects, tables and arrays for data? [import]uid: 16142 topic_id: 18286 reply_id: 74045[/import]

That’s generally what I do, unless there’s a good reason to not to, but it’s more a matter of personal preference than anything else. Whatever works best for you! [import]uid: 1294 topic_id: 18286 reply_id: 74057[/import]