简体   繁体   English

动作3:检查movieClip是否存在

[英]actionscript-3: check if movieClip exists

I have a movieclip created with the following code: 我有一个使用以下代码创建的动画片段:

var thumbContainer:MovieClip = new MovieClip();
thumbContainer.name = "thumbContainer";
stage.addChild (thumbContainer);

If the window gets larger/smaller I want everything back in place. 如果窗口变大/变小,我希望所有内容恢复原位。 So I have an stage Event Listener. 所以我有一个舞台事件监听器。 Now I want to see if this mc exists to put back in place. 现在,我想看看此mc是否存在以放回原位。 I've tried different ways but keep getting an error that does not exist. 我尝试了不同的方法,但是不断收到不存在的错误。

1120: Access of undefined property thumbContainer. 1120:访问未定义的属性thumbContainer。

if (this.getChildByName("thumbContainer") != null) {
 trace("exists")
}

and

if ("thumbContainer" in this) {
 trace("exists")
}

or 要么

function hasClipInIt (mc: MovieClip):Boolean {
 return mc != null && contains(mc);
}
stage.addChild (thumbContainer);
//...
if (this.getChildByName("thumbContainer") != null) 

You are adding the thumbContainer to stage and checking for its existence with this . 您正在将thumbContainer添加到stage并使用this检查它的存在。 Change stage to this or this to stage . 修改stage ,以thisthisstage

That said, an even more appropriate way is to keep a reference to the added movie clip and check for existence with the contains method. 也就是说,一种更合适的方法是保留对添加的影片剪辑的引用,并使用contains方法检查是否存在。 It determines whether the specified display object is a child of the DisplayObjectContainer instance or the instance itself. 它确定指定的显示对象是DisplayObjectContainer实例的子级还是实例本身。 The search includes the entire display list including this DisplayObjectContainer instance, grandchildren, great-grandchildren, and so on. 搜索包括整个显示列表,包括该DisplayObjectContainer实例,孙子孙,曾孙子孙等等。

Hence you can easily check using stage.contains(thumbContainer); 因此,您可以使用stage.contains(thumbContainer);轻松检查stage.contains(thumbContainer);

if you are having trouble firing errors, you can always resort to a try catch 如果您在发射错误时遇到麻烦,可以随时尝试使用

try{
  /// do something that will blow up...
}catch( e:Error ){
  trace( "we had an error but its not fatal now..." );
}

the problem was that 'stage' and 'this' are not the same...that's why I couldn't talk to the mc. 问题是“舞台”和“这个”不一样……这就是为什么我不能和MC交谈的原因。 this works: 这有效:

var thumbContainer:MovieClip = new MovieClip();
thumbContainer.name = "thumbContainer";
addChild (thumbContainer);
if (getChildByName("thumbContainer") != null) {
    trace("exists")
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM