Skip to main content
chienbleu
Known Participant
February 3, 2023
Answered

How Do I FULLY Remove a Child in AS3?

  • February 3, 2023
  • 2 replies
  • 1314 views

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 has been closed for replies.
Correct answer JoãoCésar17023019

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

2 replies

JoãoCésar17023019
Community Expert
JoãoCésar17023019Community ExpertCorrect answer
Community Expert
February 4, 2023

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

chienbleu
chienbleuAuthor
Known Participant
February 4, 2023

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

JoãoCésar17023019
Community Expert
Community Expert
February 5, 2023

You're welcome!

kglad
Community Expert
Community Expert
February 3, 2023

function removeChildF(obj:DisplayObject):void{

if(obj.stage){

obj.parent.removeChild(obj);

}

}

chienbleu
chienbleuAuthor
Known Participant
February 4, 2023

thank you. this looks like it's working

kglad
Community Expert
Community Expert
February 4, 2023

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