as3 check if child exists - removeChild();

Papa De Beau picture Papa De Beau · May 15, 2013 · Viewed 26.9k times · Source

The code below creates a MovieClip called "circle" and checks if it exists and deletes it via removeChild(); It removed the circle but the [object MovieClip] is still there.

How can I check if a child is "on stage" or removed using removeChild?

import flash.display.MovieClip;
import flash.events.MouseEvent;

var circle:MovieClip  = new MovieClip();
circle.graphics.beginFill(0xFF794B);
circle.graphics.drawCircle(50, 50, 30);
circle.graphics.endFill();
addChild(circle);
circle.addEventListener(MouseEvent.CLICK, test);

function test(event:MouseEvent)
{
    trace(circle);
    if(circle)
    {
     trace("Called if Circle");
     removeChild(circle);
    }
    trace(circle);
}

Answer

fsbmain picture fsbmain · May 15, 2013

check the circle.stage property:

    if(circle.stage)
    {
        trace("circle is in display list");
        circle.parent.removeChild(circle);  //remove circle from display list
        circle = null //remove reference to the circle, mark it for garbage collection
    }
    else
    {
        trace("circle isn't in display list");
    }