brenjt
brenjt

Reputation: 16297

AS3 Embed access child movieclips

So I am using this code to embed my .swf file. I am creating classes for each asset in my library.

[Embed('assets/assets.swf', symbol='game.menu.levels')]
public static const LEVELS_MENU:Class;

It works just fine but how can I access a child element multiple depths down?

I have this thus far:

this.object = new R.LEVELS_MENU();
var child_element:MovieClip = 
    this.object.getChildByName("child_element") as MovieClip;

Is there a better way than doing this:

var child_child_element:DisplayObjectContainer = 
        DisplayObjectContainer(
                child_element.getChildByName("child_child_element")
        );
var child_child_child_element:DisplayObjectContainer = 
        DisplayObjectContainer(
                child_child_element.getChildByName("child_child_child_element")
        );

Is there a way I could do it with dot syntax as such:

child_element.child_child_element.child_child_child_element...

Upvotes: 2

Views: 1959

Answers (2)

Amy Blankenship
Amy Blankenship

Reputation: 6961

If your main document Class declares the instances (i.e. "declare stage instances automatically" is off, then just cast the swf to the document Class.

So,

var yourSwf:YourSwf = new LEVELS_MENU() as YourSwf;
var mc = yourSwf.child1;

You should probably consider exposing an interface on your main document Class, so that other Classes don't need to know it's specifically a YourSwf. For an example of this, check out http://flexdiary.blogspot.com/2009/01/example-of-casting-contets-of-swfloader.html .

Note that you are setting yourself up for a world of hurt, using a static Class where Classes are coupled to it to supply their own dependencies. Please at least consider migrating to the Abstract Factory pattern (http://www.as3dp.com/2009/01/25/actionscript-30-abstract-factory-design-pattern-multiple-products-and-factories/)

Upvotes: 1

Neoraptor
Neoraptor

Reputation: 962

Not as far as I am aware.

There might be a workaround.

If you created those children from the Flash IDE, you should be able to access them directly via references like this:

MovieClip(this.object).child1.child2.child3.child4;

Or if you are doing that MovieClip initialization manually (new operator) inside the swf, you can also create those references manually like this:

child["child_child_name"] = child_child;

Though this won't matter for movieclips, as they are dynamic, so you can just do:

child.child_child_name = child_child;

These last two are inside .swf code of course;

Upvotes: 2

Related Questions