Copy link to clipboard
Copied
I'm making a game where you create springs and I want them to be fully removed when they go off stage. The only way I know how to go about this is "removeChild(thing);" but that just hides it. Is there a way that can actually delete the instance because I'm not sure how. It also displays this error which I don't fuly understand.
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at MethodInfo-49()
Hi.
Also remember that besides removing from the display list, you must remove all event listeners from the object and set it to null.
In this way you can avoid null references in the event handlers and you make sure that it can be effectively garbage collected.
Regards,
JC
Copy link to clipboard
Copied
function removeChildF(obj:DisplayObject):void{
if(obj.stage){
obj.parent.removeChild(obj);
}
}
Copy link to clipboard
Copied
thank you. this looks like it's working
Copy link to clipboard
Copied
i should hope so. (and you're welcome.)
Copy link to clipboard
Copied
oh wait sorry I got a bit confused. It's just giving some error about a null object refrence or something. I'm just not sure about how children or parents work in general
Copy link to clipboard
Copied
if you're passing a null object that's poor form, but in that if-statement you can use
if(obj && obj.stage)
and it will catch that problem
Copy link to clipboard
Copied
Hi.
Also remember that besides removing from the display list, you must remove all event listeners from the object and set it to null.
In this way you can avoid null references in the event handlers and you make sure that it can be effectively garbage collected.
Regards,
JC
Copy link to clipboard
Copied
thank you again. i forgot to remove the event listeners but now it's working smoothly
Copy link to clipboard
Copied
You're welcome!
Copy link to clipboard
Copied
Since I have a timeout on frame1, and I have addChild and addListener functions on multiple frames, is there a summary way of clearing all children and listeners that may have been created on a previous frame when pulled back to frame1 by a timeout?
Copy link to clipboard
Copied
yes, for removing children but for removing listeners it's better to loop through the listeners you use.
Copy link to clipboard
Copied
I also asked this in another thread, so I'll remove that and follow up here.
How can a child or listener that was created in another frame be accessed if a timeout has moved to frame1?
(I guess I can't delete posts, so that question is also in this thread: https://community.adobe.com/t5/animate-discussions/check-if-eventlistener-exist/td-p/3490383. I'll check either for updates.).
Find more inspiration, events, and resources on the new Adobe Community
Explore Now