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);
}
有帮助吗?

解决方案

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");
    }

其他提示

You probably want to use the contains function of a DisplayObject.

if (contains(circle))
{
    // The circle is contained by the current clip
    removeChild( circle );
    // Remove the reference to the clip 
    // (optional, if you don't want to use the circle again)
    circle = null; 
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top