View Full Version : [AS3] Nesting code in Movieclips
timf999
02-17-2009, 04:42 PM
I need to get a Movieclip to destroy (remove) itself when it has reached its final frame. I cannot do this on the main timeline because I need it to play before this happens. I tried
this.removeChild();
but that doesn't work. Help!
Thanks in advance
EightySeven
02-17-2009, 05:16 PM
MovieClip(parent).removeChild(this);
timf999
02-17-2009, 08:17 PM
MovieClip(parent).removeChild(this);
Wahoa, works like a charm! Thanks :p
Can't help wondering why it isn't in the Flash manual, though...
Because you won't always want to do it that way; you might have some kind of manager that will remove it instead, you might rather have the parent to remove its children, etc.
I notice you said "destroy (remove)" in your opening question. This does not destroy the child, it just removes it from the display list. It will only be destroyed if the display list was the only reference to the object.
EightySeven
02-17-2009, 11:59 PM
how would u recommend u destroy the object then?
bluemagica
02-18-2009, 04:08 AM
to destroy a object completely, you have to remove all references to it manually( manually means, in code you gotto specify all its references and remove it from them)! In as3, you yourself cannot exactly destroy the object, you can only help flash destroy it for you! Flash periodically will look for a object which is not being referenced anymore, and then mark it for garbage collection! so the only thing you can do, is remove all references so that flash can mark the object for garbage collection!
Bluemagica has the idea. This explains in some more detail about garbage collection.
http://www.gskinner.com/blog/archives/2006/06/as3_resource_ma.html
Now, in many circumstances, the display list is the only reference to it, and then doing a removeChild is all you need to do. But it's good to understand garbage collection so you'll know when that isn't enough.
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.