Dangers of the ActionScript default package

posted in: ActionScript 3, Flash | 0

While building an integration framework between Unity3D and Flash, I started getting runtime errors on some of the SWFs that the framework was loading. That is the “framework” has a swf which loads other swfs at runtime using the AS3 Loader class.

An example of the error was

ReferenceError: Error #1056: Cannot create property stageTitleLabel on Main.
at flash.display::Sprite/flash.display:Sprite::constructChildren()
at flash.display::Sprite$iinit()
at Main$iinit()

Where stageTitleLabel was just one of the dynamic text fields in a MovieClip that was used in the loaded swf.

Doing an extensive search of the web seemed to narrow down the cause of the problem to be one of two things.

a) Items had not been Automatically declared on the stage


b) Classes needed to be declared as “dynamic”

Yet, in my case, enabling automatic declarations and making all classes “dynamic” made no difference at all.

Removing all functionality, by erasing the name of the document class in the IDE stopped the error, but of course removed all functionality.

However I found that if the document class was not called Main, but instead Main2, that the errors vanished and that everything worked normally.

This was my light bulb moment.  The problem is that Flash appears to have a namespace clash because both the loader swf and the loaded SWF both had the same classname “Main” and both use the unnamed “default package”.

My normal build process uses Flash Develop as a code editor and compiler, and I only use the Flash IDE to integrate graphics into the SWF, so by default I disable the Automatic declaration of stage instances, and declare them all in the code. This allows Flash Develop, to compile the code using the Flex compiler, and I find makes much more robust code, due to the strict nature of the Flex compiler.

However,  in this case I went through the code and commented out all declarations of stage instances, and enabled the automatic declaration of stage instances in the Flash IDE.

I also went through all classes in the project, about 8 of them, and declared them all as dynamic.

But this made no difference what so ever to the bug 🙁

So I republished the project in the IDE, with no document class, and although there was no functionality, the SWF loaded into the framework without any errors.