group:remove returns an emtpy group?

Hi,

I would like to remove a group from one group and insert it into another group. But after removing the group, all its children are gone…
[lua]local parentDisplayGroup = display.newGroup()
local subDisplayGroup = display.newGroup()

subDisplayGroup:insert(display.newCircle(20, 20, 20))
subDisplayGroup:insert(display.newCircle(20, 20, 20))
subDisplayGroup:insert(display.newCircle(20, 20, 20))
print(subDisplayGroup.numChildren) – returns 3

parentDisplayGroup:insert(subDisplayGroup)
print(subDisplayGroup.numChildren) – still returns 3

parentDisplayGroup:remove(subDisplayGroup)
print(subDisplayGroup.numChildren) – returns nil?[/lua]
I also tried to replace the last to lines of code with:

[lua]local removedDisplayGroup = parentDisplayGroup:remove(1)
print(removedDisplayGroup.numChildren)[/lua]
But it still returns nil. [import]uid: 24216 topic_id: 5615 reply_id: 305615[/import]

If I understand the goal, you actually just need to re-insert the child group into the new parent to “remove” it from the previous parent. Calling remove() actually removes the group and its children from the display list altogether.

For example:

local parentDisplayGroup = display.newGroup()local newParent = display.newGroup()local subDisplayGroup = display.newGroup() subDisplayGroup:insert(display.newCircle(20, 20, 20))subDisplayGroup:insert(display.newCircle(20, 20, 20))subDisplayGroup:insert(display.newCircle(20, 20, 20))print(subDisplayGroup.numChildren) -- returns 3 parentDisplayGroup:insert(subDisplayGroup)print(subDisplayGroup.numChildren) -- still returns 3 -- parentDisplayGroup:remove(subDisplayGroup)newParent:insert(subDisplayGroup)print(parentDisplayGroup.numChildren) -- returns 0print(subDisplayGroup.numChildren) -- returns 3[/code] [import]uid: 8196 topic_id: 5615 reply_id: 19137[/import]

If what you want to do is move an object from one display group to another you don’t need to use remove.

From the Corona docs: http://developer.anscamobile.com/content/display-objects

That if you insert a display object into one group and then later insert the same object into another group, the display object is removed the original group into which it was inserted [import]uid: 11393 topic_id: 5615 reply_id: 19138[/import]

Thanks to both of you. Just inserting without remove seems to work. But what if i don’t have any direct references to my child groups and want to move them from one parent group to another? There wouldn’t be any other way then using remove to get a reference to the child group. But then using remove also removes the children of my child group, returning me an emtpy child group. [import]uid: 24216 topic_id: 5615 reply_id: 19140[/import]

You can create references like so:

[lua]parentGroup:insert(sub)
parentGroup[“subGroup”] = sub – Now indexed with reference subGroup

print( parentGroup.subGroup.numChildren )
print( parentGroup[“subGroup”].numChildren )[/lua]

You can do that with any table in lua.
[import]uid: 11393 topic_id: 5615 reply_id: 19144[/import]

You are are right according to the docs:

group:remove( indexOrChild ) Removes from group the display object specified by indexOrChild, shifting down other elements as needed. The argument indexOrChild is either the index position of the child within group or the child display object itself. In either case, it returns the removed display object.

But I couldn’t get it to work as remove was returning nil, not the removed object. [import]uid: 11393 topic_id: 5615 reply_id: 19145[/import]

Ah, great! I guess that’s exactly what I was looking for! [import]uid: 24216 topic_id: 5615 reply_id: 19146[/import]