Using 'as' to cast content during INIT event callback breaks Frame 0 script execution

Hey all. I’ve got a really weird problem that I’ve boiled down to its fundamentals, and I’m hoping that someone here smarter than I am knows why AS3 would be acting this way. Attached is a ZIP with a few FLAs and .AS files. I’m using Flash CS5. Please bear with me as I explain the situation.

I have a class called TestChildA:

package testing
{
import flash.display.MovieClip;

public class TestChildA extends MovieClip
{
public var test_str:String;
public function TestChildA()
{
test_str = “DEFAULT A”;
}
}
}

Stupidly simple. I have another one called TestChildB that does exactly the same thing, but sets test_str to “DEFAULT B”. Both of these classes have been bound to separate FLAs as the Document Class. In frame 0 of TestChildA.FLA, this ActionScript is present:

trace(“Test Child A Frame 0”);
test_str = “TestChildA”;
stop();

TestChildB’s FLA has the same ActionScript with ‘A’ replaced with ‘B’.

I also have a class called TestRoot. In the TestRoot constructor, TestChildA.swf and TestChildB.swf are both loaded with separate Loader instances. Both are hooked to the same INIT event callback. The INIT callback looks like this:

private function load_init(evt:Event):void
{
trace("Load Init: " + evt.currentTarget.loader.contentLoaderInfo.content.test_str);
}

Running TestRoot works fine in this case. The output is:

Test Root Frame 0
Test Child A Frame 0
Test Child B Frame 0
Load Init: TestChildB
Load Init: TestChildA

As one would expect, the Frame 0 scripts for both Child A and Child B are executed prior to the INIT callback firing. No problem there.

However, let’s change the INIT callback to this:

private function load_init(evt:Event):void
{
var test_a:TestChildA = evt.currentTarget.loader.contentLoaderInfo.content as TestChildA;
var test_b:TestChildB = evt.currentTarget.loader.contentLoaderInfo.content as TestChildB;

if( test_a != null )
trace("Load Init: " + test_a.test_str);
if( test_b != null )
trace("Load Init: " + test_b.test_str);
}

All we’re doing is now obtaining references to actual TestChildA and TestChildB objects with the ‘as’ keyword. Running TestRoot after this results in the following output:

Test Root Frame 0
Load Init: DEFAULT B
Load Init: DEFAULT A

As you can see, although the Child A and Child B SWFs have been loaded, their Frame 0 script has completely failed to run. Their test_str values are still the default value specified in their constructors.

Here’s the fun part. If we comment out the acquisition of the reference to Child A and the subsequent trace of its test_str, we get the following output:

Test Root Frame 0
Test Child A Frame 0
Load Init: DEFAULT B

For some reason, the fact that we’re not trying to obtain a TestChildA reference allows the Frame 0 ActionScript in TestChildA.swf to execute properly. TestChildB, on the other hand, continues to fail Frame 0 execution since we’re still getting a reference to it.

As we saw in the first version of the INIT handler above, querying the test_str variable out of the loaded content directly without trying to cast it worked fine. Why is it that using ‘as’ to get a specific reference to the content with the proper class results in the Frame 0 script of that content to fail running? Theoretically, the Frame 0 actions should have already been executed prior to reaching the INIT handler anyway, so it’s doubly confusing.

If anyone has a better understanding than I of the ActionScript internals and can explain the reason for this issue, I would appreciate it. I’ve been banging my head against the wall for two days trying to figure this out.

Thanks,
Dan