Removing display objects from the screen

If I have a table that contains display objects like this:

localGroup = display.newGroup() -- Standard Director display group  
  
local objects = {}  
  
objects[1] = display.newImage("SomeImage.png")  
objects[1].x = 100  
objects[1].y = 100  
localGroup:insert(objects[1])  

Then I should be able to remove the object from the screen like this:

localGroup:remove(objects[1])  
objects[1] = nil  

Is that correct? Are there any other steps I need to perform to remove an object from the screen.

My actual code is a little more complex, but follows the above approach as far as I can tell.

I have a table to store display objects and a function to remove/re-draw objects:

  
local localGroup = display.newGroup()  
  
-- Multi-dimensional array to store display object data  
local mRequired = {}  
mRequired[1] = {}  
mRequired[2] = {}  
mRequired[3] = {}  
-- Draw's display objects and adds them to mRequired based on the value in rodNo  
local function draw(rodNo, xPosStart)  
  
 -- Remove display objects if any already present  
 for i, v in ipairs(mRequired[rodNo]) do  
 if (mRequired[rodNo][i] ~= nil) then  
 localGroup:remove(mRequired[rodNo][i])  
 mRequired[rodNo][i] = nil  
 end  
 end  
  
 -- Add the new display objects  
 -- Numbers is another table containing 1..n items which are image filenames  
 -- The objects drawn are different depending on the filenames  
 -- I can see the old images showing through beneath the new images though  
 -- This is my problem; how to remove the old images  
 local numbers = getTableOfFilenames()  
 local xPos = xPosStart  
 local yPos = 100  
 for i, v in ipairs(numbers) do  
  
 mRequired[rodNo][i] = display.newImage(v, true)  
 mRequired[rodNo][i]:setReferencePoint(display.TopLeftReferencePoint)  
 mRequired[rodNo][i].x = xPos  
 mRequired[rodNo][i].y = yPos  
  
 localGroup:insert(mRequired[rodNo][i])  
  
 xPos = xPos + 20  
  
 end  
  
end  
  
-- The code is called like this  
draw(1, 100)  
draw(2, 200)  
draw(3, 300)  
  
-- The function is called multiple times throughout the lifetime of my game's levels  
  

So, to be clear, the function correctly displays the images as I want it to. But, on subsequent calls to the function the old images are not removed from the display. Hence the new images are displayed over the top and, as you can imagine, this eventually becomes a mess of imagery. [import]uid: 26769 topic_id: 12237 reply_id: 312237[/import]

Why can I not solve these problems, and then when I post to the forum I immediately think, “Oh, that’s the solution!”? :slight_smile:

My problem is that in another part of the code (which I didn’t include for brevity, but actually turned out to be the culprit!), I was adding a new image to the mRequired table without incrementing the element pointer first.

So, in effect I was doing something like:

mRequired[1][1] = display.newImage("SomeImage.png")  

And then when adding a final image, I was doing:

mRequired[1][1] = display.newImage("FinalImage.png")  

I should have been doing:

mRequired[1][2] = display.newImage("FinalImage.png")  

In other words I now had two images displayed, but only 1 reference stored in my mRequired. So, when removing images based on the contents of mRequired I only removed the one image.

All sorted now.

[import]uid: 26769 topic_id: 12237 reply_id: 44581[/import]

Heh, well done on fixing it yourself :slight_smile: It’s always a good feeling.

That used to happen to me a lot too - post, 2 seconds later something just clicks. [import]uid: 52491 topic_id: 12237 reply_id: 44662[/import]