Skip to main content
Inspiring
December 3, 2015
Question

error shows up after removing child

  • December 3, 2015
  • 2 replies
  • 2759 views

so when evil dies it plays a death animation and is removed on the last frame

it works fine but an error show up

The supplied DisplayObject must be a child of the caller.

if(evil.currentFrame == 47)

   {

    if(contains(evil))

    {

     removeChild(evil);

    }

    if(contains(evilShadow))

    {

     removeChild(evilShadow);

    }

   }

This topic has been closed for replies.

2 replies

kglad
Community Expert
December 3, 2015

use:

if(evil.currentFrame == 47)

   {

     removeF(evil);

     removeF(evilShadow);

   }

function removeF(dobj:DisplayObject=null):void{

if(dobj&&dobj.stage){

dobj.parent.removeChild(dobj);

}

}

Inspiring
December 3, 2015

I am not sure but I think he's using the if statement on enter frame event.. so if he used if(evil.currentFrame == 47) first then he'll get an error after it has been removed..

Inspiring
December 3, 2015

i'm also pretty sure that code is within a loop (like enterfrmae), but he will still see an error with your code and not with my code.  ie, removing a movieclip from the display doesn't change its frame or even whether its timeline continues to loop.

i believe he's getting that error because evil or evilShadow aren't children of 'this' when that error is triggered.  ie, contains() returns true if the argument is a descendant of 'this' but it may not be a child of 'this'.

one solution is to use evil.parent.removeChild(evil) etc.


Actually I've tested the codes and it's all working well no errors even his code..

so I think that the "evil" object is not a child of "this" as you said.. you're right.

Inspiring
December 3, 2015

if(contains(evil))

   {

    if(evil.currentFrame == 47)

    {

     removeChild(evil);

     removeChild(evilShadow);

    }

   }