views:

1055

answers:

3

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);
}
+3  A: 
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);

Amarghosh
Well my check works, so I can check if the mc exists, but now I want to do something with it. This (instance name) wont work:var thumbContainer:MovieClip = new MovieClip();thumbContainer.name = "test";thumbContainer.x = 10;stage.addChild (thumbContainer);if (this.getChildByName("test") == null) { trace ("something"); //Works! this.getChildByName("test").x = 10; //Works not! stage.test.x = 10; //Works not!}
Edwinistrator
A: 

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")
}
Edwinistrator
A: 

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..." );
}
David Morrow
that's a nice trick!
Edwinistrator