Thursday, July 24, 2008

AS3 Notes: Automatically Declare Stage Instances

I meant to touch on this awhile back, but there is a helpful AS3 feature buried in the ActionScript publish settings called "Automatically declare stage instances". By Default it is always set to true. I have found that when I am designing in the Flash IDE and scripting in Flex3 that I am better off without this feature enabled.

Basically the Auto Declare feature means that any movieclip on the stage will automatically be declared in the class containing the movieclip. For example, if you place a movieclip of a circle on the stage and give it an instance name of "ball" then the document class will automatically declare a variable called ball similar to you typing in "public var ball:MovieClip;".

The problem with this (at least in my development setup) is that I can not make references to the ball variable in Flex since it does not get declared until runtime. If I try to help Flex by declaring a new variable called ball I will get a duplicate variable exception when the code compiles. I could always cheat and make a variable with a different name such as "ball_mc" and then target the movieclip by writing the following: var ball_mc:MovieClip = this["ball"]; It works, but now you have 2 references for every movieclip and it also looks pretty hacktastic.

To get around this problem, go into the action script settings and turn off the Automatically Declare Stage Instances. In Flex at the top of your document class declare the following variable: public var ball:MovieClip; It is very important to declare all movieclips that are in your Flash IDE as public, otherwise you will get an exception when it tries to compile. By following this setup, you will now have 1 reference to the ball movieclip and you do not need to do any hacktastic tricks to gain access to it. The only hassle I see is trying to maintain the instance in two places, but I don't see any way around that until Flex and Flash are integrated.

5 comments:

Troy said...

Thanks for this tip. Could you provide some more details on your Flex/Flash setup? I've not been able to find a workflow that lets me leverage the best of both worlds. Obviously, the Flex editor and debugger are superior, but my clients usually want my work delivered as a FLA their artists can re-skin and build (without using Flex).

Ickydime said...

I've found two that I like so far.

The first I am more comfortable with, the second I just started using in my last two projects.

1. Create an ActionScript Project in Flex. Create a "classes" folder. Within that folder create a package folder for your project that contains a Default Application Class. Once you have that setup you should be good to go on the coding side.

On the Flash side, make sure you import the "classes" folder in your publish settings. Then declare the "Default Application" class as your document class.

Compile the swf in the Flash IDE and you are ready to roll. Any new Movieclips can be linked to classes using the Linkage Property. You won't need to put a single line of code in the Flash IDE.


2. Start the exact same way as number one. Create the ActionScript project and the Default Application file. This time, however, we are going to compile the swf in Flex so do not set the document class in the Flash IDE. Instead, just make sure all your assets are available on frame 1 and are linked properly to your classes in Flex, then publish the swf. Within Flex, have your main AS class load the swf and then access the assets by requesting their Definitions.

This second setup is a little more tricky, but with it you get the added bonus of being able to use to Flex Profiler.

kinetic_one said...

Hey,Mark


I had te same issue lately with a flash designer.
Althoug, I always left the option checked and did not have any problem in FB.
I like ickydime's second method.
This way, you only use the flash IDE a s a library.
The thing is, if you don't ! put items on the stage in your Flash IDE, than this problem won't occur, and you can control them completely through you actionscript in FB, declaring them and keeping them private wherever you want.

wagster said...

I compile with Flex Builder - although I have in the past used Flash to compile while coding in Flex. I'm planning to try another workflow though - set up so that you can compile from either. Shouldn't be that hard really, so long as your "application file" in FB is the same as your "Document Class" in Flash and all your classpaths are correct. That means you can just compile from whatever app you are working in, depending on what you are doing at the time.

Oh - here's another little tip - this drove me crazy for a while. If there are any problems with the swc, Flex Builder will just report the dreaded "Internal Build Error". If you get that, try compiling the swc again from Flash and look for some sensible errors there.

Anonymous said...

Thanks Ickydime. This is my first time integrating asdocs, flashbuilder, and the flash IDE and this tip helped me sort out issues I was having with IDE based items.

Thanks!