1

下面的代码创建了一个名为“circle”的影片剪辑,并检查它是否存在并通过 removeChild() 将其删除;它删除了圆圈,但 [object MovieClip] 仍然存在。

如何检查孩子是“在舞台上”还是使用 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);
}
4

2 回答 2

6

检查circle.stage属性:

    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");
    }
于 2013-05-15T07:20:25.500 回答
4

您可能想要使用containsDisplayObject 的功能。

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; 
}
于 2013-05-15T09:04:26.760 回答