Afaik you can have an display object only in one group.
When you create an display object it will be initially in the stage group (always)
If you create your own group and insert your object into that group it will be removed from its current parent automatically.
I was also struggling when I first read about those stuff. It is not clear from the documentation for me neither.
Esp. those advices to obj=nil after a obj.parent:remove(obj) in the documentation is asking for confusion.
You do not need to nil the object … you need lose all references to the object. Having the object in the stage or any group is just having a reference to it somewhere.
If you use a display group for holding your objects … you won’t need to nil em.
grp=display.newGroup()
r1=display.newRect(...)
-- here r1 is child of stage
grp:insert(r1)
-- here r1 is only child of group
r2=display.newRect(...)
grp:insert(r2)
-- at this state grp is child of stage
-- r1 and r2 is child of grp
-- and r2 is above r1 in the display
parent.r1:insert(r1)
-- grp:insert(r1) would be equivalent!
-- now r1 is on top if r2 .. still child in grp
r1.parent:remove(r1)
-- now the rect object r1 is removed from the display
-- but it is still allocated because r1 is referencing it
r1=nil
-- now the rect object is gone
So far for those examples… BUT
In real life situations you may just add the display object to a group which was created with the use of a local variable. I do this a lot…
Imagine you create dice faces. I would create 6 groups and every one of this groups is getting dots.
But I would not keep track of the objects which represent the dots.
I just keep track of the groups they are in.
Now imagine you want to change the image you used for the dots with another one.
Now you iterate backwards through the group and call obj.parent.remove(obj) for every single dot and add the new ones. You do not “nil” anything. As there are no references to those objects.
Another example… I create my buttons roughly like this:
function Foo:createButtons()
self.butA = display.newGroup()
local tmp=display.newRect(0,0,...)
tmp:setFillColor(...)
butA:insert(tmp)
-- button has now a background
tmp=display.newText(5,5,...)
tmp.setTextColor(...)
butA:insert(tmp)
-- text is over background now
-- move the button somewhere (all stuff in the group)
butA.x=100
butA.y=120
end
-- to change the color of the button background
self.butA[1]:setFillColor(..)
-- to change the text color of the button
self.butA[2]:setTextColor(..)
You can see… the display group is at the same time the “storage” for the objects.
If you remove them… they are going to be deallocated because this is the only reference to them.
To implement two different pages you just
page1 = display.newGroup()
page2 = display.newGroup()
-- remove page2 from the stage...
page2.parent:remove(page2)
...
-- Switch to page2
display.getCurrentStage():insert(page2)
page1.parent:remove(page1)
Correct me if I am wrong somewhere please
[import]uid: 6928 topic_id: 1100 reply_id: 3241[/import]