How to remove an array of objects?

Hello,

I have several images added to the screen such as imgPerson[1], etc. Is there a simple way to do a selfRemove on all of them instead of calling each one individually?

Thanks,

Warren

 

If you have added them to a display group (the proper way of handling arrays of images) you would simply need to remove the display group and the images would be removed with it. The only thing you need to be careful of is that you don’t have any references to those images elsewhere.

horacebury, 

I have a question regarding the use of display groups for arrays of images. 

1.) I create an array of ten images

2.) I create a display group to hold each image. 

3.) I add each image to the display group.

4.) At some point I need to remove the images from the screen, so I remove the display group. 

The above works great. My question is should each table reference be removed and set to nil as well? Here’s a code example I created:

local imageTable = {} -- a table to hold our images local imageGroup = display.newGroup() -- a display group to hold our table local centerX = display.contentCenterX local centerY = display.contentCenterY local mRand = math.random -- localize the math.random function for faster processing -- loop to create 10 rectangles in imageTable. Add them to the display group for i = 1, 10 do imageTable[i] = display.newRect(40,40,40,40) imageTable[i]:setFillColor(mRand(0,9),mRand(0,9),mRand(0,9)) imageGroup:insert(imageTable[i]) end local function removeImages() display.remove(imageGroup) -- remove the display group -- loop through the image table and remove each reference; set to nil for i = 1, #imageTable do imageTable[i]:removeSelf() imageTable[i] = nil -- print to console to check that each is removed if imageTable[i] == nil then print("Image ".. i .. " removed!") end end end -- function to move the images local function moveImages() -- transition each image to the center of the screen, .5 seconds between moves. after the 10th image has moved, run function removeImages() transition.to(imageTable[1], {time = 500, x = centerX, y = centerY } ) transition.to(imageTable[2], {time =1000, x = centerX, y = centerY} ) transition.to(imageTable[3], {time =1500, x = centerX, y = centerY} ) transition.to(imageTable[4], {time =2000, x = centerX, y = centerY} ) transition.to(imageTable[5], {time =2500, x = centerX, y = centerY} ) transition.to(imageTable[6], {time =3000, x = centerX, y = centerY} ) transition.to(imageTable[7], {time =3500, x = centerX, y = centerY} ) transition.to(imageTable[8], {time =4000, x = centerX, y = centerY} ) transition.to(imageTable[9], {time =4500, x = centerX, y = centerY} ) transition.to(imageTable[10], {time =5000, x = centerX, y = centerY, onComplete = removeImages} ) end moveImages()

If you look at the removeImages() function, I removed the display group, which removed the images from the screen, but I also removed each table reference and set them to nil. I’m wondering what happens to the table behind the scenes after I remove the display group. Do I need to remove each table reference and set them to nil, or is that just simply overkill?

Warren,

for i = #imgPerson, 1, -1 do

      imgPerson[i]:removeSelf()

      imgPerson[i] = nil

end

Rob

James,

Yes, you need to remove all references to any object in Lua to get it removed from memory. If you do not remove all of your table references to your images as well as removing their parent display group, they will stay in memory. That goes for any object.

Because a display group is just another table, and the image creation functions can all take a display group as their first parameter, you don’t actually need to load them into a table first. You can simply run through your creation loop, adding the images, rects or whatever directly to the display group. If you need to loop through them later you can simply iterate through the group, using 1 to group.numChildren. When it comes time to remove them all, just call removeSelf() on the group and set the variable for the group to nil.

Example:

-- create parent group local group = display.newGroup() -- create parent group to contain the display objects -- create rects for i=1, 10 do display.newRect( group, i\*10, i\*20, 10, 10 ).fill = {1/i,.5,.8} -- create rectangles end -- do something with the images for i=1, group.numChildren do transition.to( group[i], { time=math.random(1,10)\*1000, x=math.random(0, 500), y=math.random(0, 500) } ) -- randomly move each display object in the display group around end -- cleanup timer.performWithDelay( 10\*1000, function() group:removeSelf() -- removes the graphics memory used by the display group group = nil -- removes the memory reference to the display group object end, 1 )

If you have added them to a display group (the proper way of handling arrays of images) you would simply need to remove the display group and the images would be removed with it. The only thing you need to be careful of is that you don’t have any references to those images elsewhere.

horacebury, 

I have a question regarding the use of display groups for arrays of images. 

1.) I create an array of ten images

2.) I create a display group to hold each image. 

3.) I add each image to the display group.

4.) At some point I need to remove the images from the screen, so I remove the display group. 

The above works great. My question is should each table reference be removed and set to nil as well? Here’s a code example I created:

local imageTable = {} -- a table to hold our images local imageGroup = display.newGroup() -- a display group to hold our table local centerX = display.contentCenterX local centerY = display.contentCenterY local mRand = math.random -- localize the math.random function for faster processing -- loop to create 10 rectangles in imageTable. Add them to the display group for i = 1, 10 do imageTable[i] = display.newRect(40,40,40,40) imageTable[i]:setFillColor(mRand(0,9),mRand(0,9),mRand(0,9)) imageGroup:insert(imageTable[i]) end local function removeImages() display.remove(imageGroup) -- remove the display group -- loop through the image table and remove each reference; set to nil for i = 1, #imageTable do imageTable[i]:removeSelf() imageTable[i] = nil -- print to console to check that each is removed if imageTable[i] == nil then print("Image ".. i .. " removed!") end end end -- function to move the images local function moveImages() -- transition each image to the center of the screen, .5 seconds between moves. after the 10th image has moved, run function removeImages() transition.to(imageTable[1], {time = 500, x = centerX, y = centerY } ) transition.to(imageTable[2], {time =1000, x = centerX, y = centerY} ) transition.to(imageTable[3], {time =1500, x = centerX, y = centerY} ) transition.to(imageTable[4], {time =2000, x = centerX, y = centerY} ) transition.to(imageTable[5], {time =2500, x = centerX, y = centerY} ) transition.to(imageTable[6], {time =3000, x = centerX, y = centerY} ) transition.to(imageTable[7], {time =3500, x = centerX, y = centerY} ) transition.to(imageTable[8], {time =4000, x = centerX, y = centerY} ) transition.to(imageTable[9], {time =4500, x = centerX, y = centerY} ) transition.to(imageTable[10], {time =5000, x = centerX, y = centerY, onComplete = removeImages} ) end moveImages()

If you look at the removeImages() function, I removed the display group, which removed the images from the screen, but I also removed each table reference and set them to nil. I’m wondering what happens to the table behind the scenes after I remove the display group. Do I need to remove each table reference and set them to nil, or is that just simply overkill?

Warren,

for i = #imgPerson, 1, -1 do

      imgPerson[i]:removeSelf()

      imgPerson[i] = nil

end

Rob

James,

Yes, you need to remove all references to any object in Lua to get it removed from memory. If you do not remove all of your table references to your images as well as removing their parent display group, they will stay in memory. That goes for any object.

Because a display group is just another table, and the image creation functions can all take a display group as their first parameter, you don’t actually need to load them into a table first. You can simply run through your creation loop, adding the images, rects or whatever directly to the display group. If you need to loop through them later you can simply iterate through the group, using 1 to group.numChildren. When it comes time to remove them all, just call removeSelf() on the group and set the variable for the group to nil.

Example:

-- create parent group local group = display.newGroup() -- create parent group to contain the display objects -- create rects for i=1, 10 do display.newRect( group, i\*10, i\*20, 10, 10 ).fill = {1/i,.5,.8} -- create rectangles end -- do something with the images for i=1, group.numChildren do transition.to( group[i], { time=math.random(1,10)\*1000, x=math.random(0, 500), y=math.random(0, 500) } ) -- randomly move each display object in the display group around end -- cleanup timer.performWithDelay( 10\*1000, function() group:removeSelf() -- removes the graphics memory used by the display group group = nil -- removes the memory reference to the display group object end, 1 )