Skip to main content
chienbleu
Known Participant
Feb 3, 2023 11:22 PM UTC
Answered

How Do I FULLY Remove a Child in AS3?

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()

This topic is closed to new replies. Start a new post to keep the conversation going.
JoãoCésar17023019
JoãoCésar17023019Correct answer
Community Expert
Feb 4, 2023 1:28 PM UTC

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

11 replies

JoãoCésar17023019
Community Expert
JoãoCésar17023019Community ExpertCorrect answer
Community Expert
Feb 4, 2023 1:28 PM UTC

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

Translate
chienbleu
chienbleuAuthor
Known Participant
Feb 4, 2023 3:41 PM UTC

thank you again. i forgot to remove the event listeners but now it's working smoothly

Translate
JoãoCésar17023019
Community Expert
Community Expert
Feb 5, 2023 4:35 PM UTC

You're welcome!

Translate
kglad
Community Expert
Community Expert
Feb 3, 2023 11:56 PM UTC

function removeChildF(obj:DisplayObject):void{

if(obj.stage){

obj.parent.removeChild(obj);

}

}

Translate
chienbleu
chienbleuAuthor
Known Participant
Feb 4, 2023 1:54 AM UTC

thank you. this looks like it's working

Translate
kglad
Community Expert
Community Expert
Feb 4, 2023 1:59 AM UTC

i should hope so.  (and you're welcome.)

Translate