Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
1

How Do I FULLY Remove a Child in AS3?

Participant ,
Feb 03, 2023 Feb 03, 2023

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

TOPICS
ActionScript , Code , Error , Performance
1.3K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Feb 04, 2023 Feb 04, 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

Translate
Community Expert ,
Feb 03, 2023 Feb 03, 2023

function removeChildF(obj:DisplayObject):void{

if(obj.stage){

obj.parent.removeChild(obj);

}

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 03, 2023 Feb 03, 2023

thank you. this looks like it's working

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 03, 2023 Feb 03, 2023

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 03, 2023 Feb 03, 2023

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 03, 2023 Feb 03, 2023

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 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 04, 2023 Feb 04, 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 04, 2023 Feb 04, 2023

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 05, 2023 Feb 05, 2023

You're welcome!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Apr 18, 2023 Apr 18, 2023

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 18, 2023 Apr 18, 2023

yes, for removing children but for removing listeners it's better to loop through the listeners you use.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Apr 18, 2023 Apr 18, 2023
LATEST

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines