moving an display object from one display group to another

Is there a way that I can move display object from one display group to another? I tried something like this:
local z = display.newImage(“images/telephone.png”,200,200)
groupLayer01:insert(z)

z.parent:remove(z)
groupLayer02:insert(z)

I’ve got an error: bad argument #-2 to ‘insert’ (Proxy expected, got nil)

What I am trying to do is to hop the display object from display group to display group depends on different criteria. Any suggestions? Thanks.

[import]uid: 8635 topic_id: 8970 reply_id: 308970[/import]

Since groups are just a fancy name for a table, you can try this (let me know if it works for you as I’ve never personally done this in a real-life scenario):

[blockcode]
local parentGroup = z.parent

for i=1,parentGroup.numChildren,1 do
if parentGroup[i] == z then
table.remove( parentGroup, i )
end
end

groupLayer02:insert( z )
[/blockcode]

The first block of code will iterate through all children of z’s parent group, and once it finds z, it’ll remove it from the table (parent).

The last line of code insert’s z into the new group you want it to be in.

The reason why your code wouldn’t work is because the remove() function (not table.remove(), but object:remove()) will actually destroy a display object, not just remove it’s index from it’s position in a group.

Your line:

[blockcode]
z.parent:remove(z)
[/blockcode]

…was actually destroying the z object, which is why you were getting the error (proxy expected, got nil). Basically, it was expecting an object when you called insert() and got nothing. [import]uid: 52430 topic_id: 8970 reply_id: 32751[/import]

Thanks Johnathan, your suggestion works!

By the way, I modified the object:nextFrame() function in beebegame class to be like this:

 function object:nextFrame( shouldFlip )  
 -- shouldFlip is optional; if set to true, object will "flip" to next frame  
  
 --local theFrame = currentFrame  
 --local endFrame = frameCount  
 if startFrame == nil then startFrame = 1 end  
 if endFrame == nil then endFrame = frameCount end   
 local theFrame = currentFrame   
  
 if shouldFlip then  
 local fxTime = 300  
 local setInvisible = function() frame[currentFrame].isVisible = false; end  
  
 local flipTween = transition.to( frame[currentFrame], { time=fxTime, xScale=0.001, onComplete=setInvisible } )  
 else  
 frame[currentFrame].isVisible = false --\> hide current frame  
 end  
  
 if theFrame \>= endFrame then --\> on last frame, cycle back to the first  
 theFrame = startFrame   
 currentFrame = theFrame  
 else --\> not on last frame, go to next  
 theFrame = theFrame + 1  
 currentFrame = theFrame  
 end  
  
 if shouldFlip then  
 local fxTime = 300  
 local setVisible = function() frame[currentFrame].isVisible = true; end  
  
 local flipTween = transition.to( frame[currentFrame], { time=fxTime, xScale=1.0, onComplete=setVisible } )  
 else  
 frame[currentFrame].isVisible = true  
 end  
 end  

and I added

local startFrame  
local endFrame  
 function object:setStartEndFrames(s,e)  
 startFrame = s  
 endFrame = e  
 end  

so that I can select what frames to play in the movie-clip depends on my character movement. Not sure if you want to include this feature into beebegame class. Oh and thank you for creating beebegame class which it help a lot in my game developement!

[import]uid: 8635 topic_id: 8970 reply_id: 32949[/import]

Doesn’t group:insert(x) automatically remove x from x.parent?
Objects cannot be in two groups at once.
:slight_smile: [import]uid: 34945 topic_id: 8970 reply_id: 35203[/import]

It does. Shame on jonathan for not knowing it :slight_smile: [import]uid: 51516 topic_id: 8970 reply_id: 35208[/import]

i guess we can’t all be lua gods :slight_smile: j/k
[import]uid: 34945 topic_id: 8970 reply_id: 35209[/import]

Yup. But this is Corona-related, it has nothing to do with how Lua handles tables. [import]uid: 51516 topic_id: 8970 reply_id: 35211[/import]

i totally meant corona. sigh.