Swapping display objects....

I’ve seen plenty of examples of where you want to override an existing object by making it something else. For instance, the original Retina Text work around:

orignalNewText = display.newText  
display.newText = function()  
 -- do some stuff  
 originalNewText(blah blah blah)  
end  

So I need to “morph” my avatar in a game from one display object to another. But this behavior doesn’t seem to be working the way I expect it too…

function swapPlayer()  
 local oldPlayer = player  
 player = newPlayer  
 oldPlayer:removeSelf()  
 oldPlayer = nil  
end  

player and newPlayer are both movieclip.newAnim’s.

So the way I understand it, since these are all just pointers, oldPlayer should hold the player object. Then player will hold my newplayer and I should be able to safely dispose of oldPlayer.

But as soon as I do the :removeSelf() on oldPlayer, both of my objects go away.

Any thoughts on this?
[import]uid: 19626 topic_id: 22095 reply_id: 322095[/import]

I love self-solving problems. We decided to not do this and go with plan b.
[import]uid: 19626 topic_id: 22095 reply_id: 87855[/import]

@rob - that`s interesting way of coding.

So after looking your snippet above I think that we haven`t a way of “copying”(pointing) the value of the first variable (in this case you variable called "oldPlayer) to be kept on the second variable “player”, is that what you found?

PS: Btw you say that oldPlayer is a LOCAL variable, so would change something if instead you use a Global one? Just speculation. :wink:

PS2: And what about you do not NIL the variable “oldPlayer” so instantly and try to NIL it a little later or maybe when the GC works? Other speculation. .)

Cheers,
Rodrigo. [import]uid: 89165 topic_id: 22095 reply_id: 87909[/import]

I’ll have to get back to it. I’m on a bit of a time deadline with this project (and to make matters worse, I decided to gut my collision detection…)

[import]uid: 19626 topic_id: 22095 reply_id: 87917[/import]

Thats OK @rob. No worry. I can wait you get it done and after we can talk about this code again. Best of Luck there and sure youll GET IT! :slight_smile:
Regards,
Rodrigo. [import]uid: 89165 topic_id: 22095 reply_id: 87926[/import]