如何检查舞台上是否已存在某些内容?它不应该打印出第一个“不存在”而第二个“存在”吗?但它打印出两个“不存在”。
我添加了一个计时器,因为我认为需要等待一段时间才能添加到舞台上,但它不起作用。
var idonnoe:TextField = new TextField();
if (Boolean(this.getChildByName('idonnoe')))
{
trace("exists");
}
if (!Boolean(this.getChildByName('idonnoe')))
{
trace("doesn't exist");
}
addChild(idonnoe);
idonnoe.text = "hello";
var delay1:Timer = new Timer(1000, 1);
delay1.start();
delay1.addEventListener(TimerEvent.TIMER_COMPLETE, afterDelay);
function afterDelay(e:TimerEvent) :void {
if (Boolean(this.getChildByName('idonnoe')))
{
trace("exists");
}
if (!Boolean(this.getChildByName('idonnoe')))
{
trace("doesn't exist");
}
}
答案 0 :(得分:1)
getChildByName
方法考虑了myDisplayObject.name
属性,而不是指向它的变量的名称。尝试设置属性,它现在应该以您搜索它的方式存在。
idonnoe.name = "idonnoe";
答案 1 :(得分:0)
直接引用您的对象更为常见。这使得处理这种情况变得更容易。 DisplayObjectContainer的'contains(displayObject:DisplayObject)
'方法非常方便查找对象是否附加到显示列表中。
var displayObject:TextField = new TextField(); // any sublclass of DisplayObject
addChild(displayObject);
// test if the current display list contains the sprite
trace( contains(displayObject) );
// test if the sprite is attached to the stage
trace( displayObject.stage != null );
// test if the sprite is attached to ANY display list
trace (displayObject.parent != null );