下面的代码创建一个名为“circle”的MovieClip并检查它是否存在并通过removeChild()删除它;它删除了圆圈,但[对象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);
}
答案 0 :(得分:5)
检查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");
}
答案 1 :(得分:4)
您可能希望使用DisplayObject的contains
函数。
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;
}