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]
