**UPDATE:
Thanks to Robert Penner, Xtian, Jeremy, Val and Wayne for the testing. Jeremy got the answer: var smallMc:MovieClip = large_mc.getChildByName(“small_mc”) as MovieClip
trace(“works ” + smallMc); I know I have done that in the past but for some reason today it was killing me! View the comments thread for some good detail.
[Original Post Follows]
Today I ran upon the strangest thing I have seen in awhile – actually, I am surprised that I have not run upon it before. I had my friend Xtian try to replicate this as well and he was able. Therefore, I thought I would run it bast the awesome members of the Flash blogosphere.
The Scenario
Flash CS3, Exporting as Flash 9, AS3
I have a MovieClip on the stage with an instance name of large_mc. large_mc contains a clip with an instance name of small_mc. small_mc contains a clip with an instance name of verySmall_mc.
The Problem
I want to target small_mc and have it return as a MovieClip. Now, we all know that in AS3 in general, objects return to us as DisplayObjects. In this scenario I assumed that I could do the following:
MovieClip(large_mc.small_mc);
//or
large_mc.small_mc as MovieClip;
These methods return the following:
[object small_mc_2]
Now, granted AS3 is newer and I haven’t seen it all – but where the heck is that basic object coming from and why is it adding _2 to my instance name? So I thought I would exhaust all of my methods and try the following trace actions:
//traces object
trace(large_mc);
//traces object
trace(large_mc.small_mc);
//traces MovieClip (last level of nesting)
trace(large_mc.small_mc.verySmall_mc);
trace(“—————”);
//traces object
trace(MovieClip(large_mc));
//traces object
trace(MovieClip(large_mc.small_mc));
//traces MovieClip (last level of nesting)
trace(MovieClip(large_mc.small_mc.verySmall_mc));
trace(“—————”);
//traces object
trace(large_mc as MovieClip);
//traces object
trace(large_mc.small_mc as MovieClip);
//traces MovieClip (last level of nesting)
trace(large_mc.small_mc.verySmall_mc as MovieClip);
Apparently if you crawl all the way through the nest to the final nested clip, verySmall_mc, you finally get a MovieClip.
The Question
Besides, “What the heck is going on here” I want to know how you would target the small_mc clip nested within large_mc. Basically the AS3 equivalent to the AS2 version of the code: large_mc.small_mc.
Another Kicker and Strange Point
Go into your library and assign the MovieClips to export for ActionScript. Watch the erros fly as the compiler has no idea what small_mc_2 is.
Xtian found that this code will return a reference to small_mc:
trace(large_mc.getChildAt(1));
Now THAT is messed up – childAt(0) returns Shape and childAt(1) returns MC – is this pulling the children from the layer/level stack it is on the physical timeline? Now this is all fine and good but what if there are multiple nested clips within large_mc – how do I know which is Child(0).
Any help appreciated – thanks to Xtian for finding the export issue as well.
Download the Example
You can download my example FLA here.