简体   繁体   中英

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. I've tried different ways but keep getting an error that does not exist.

1120: Access of undefined property 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 . Change stage to this or this to stage .

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. It determines whether the specified display object is a child of the DisplayObjectContainer instance or the instance itself. The search includes the entire display list including this DisplayObjectContainer instance, grandchildren, great-grandchildren, and so on.

Hence you can easily check using 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. this works:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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