Flash & Flex Integration Gotcha: Automatically Declare Stage Instances

I’ve gotten a lot of emails asking about some of my older Flash & Flex integration examples not working. Hopefully this article will describe the same error everyone is getting.

ActionScript 3 is strict. However, Flash CS3 helps Flash developers with a feature that automatically generates code during compilation. Meaning, if you have a MovieClip on the stage called “my_anime”, you won’t have to declare a variable like so:

public var my_anime:MovieClip;

Flash CS3 will automatically write that in the code for you.

If you didn’t have that code, you’ll get a runtime exception. That’s because the timeline is effectively going:

yourMovieClipInstance.my_anime = new MovieClip();

Since yourMovieClipInstance’s class doesn’t have a property called “my_anime”, it’ll blow up. For a Flex Developer, this makes sense. Everything exists, and you merely addChild/removeChild or toggle the visible property. In Flash, things may not exist till frame 10. For some animations that have a ton of child MovieClips that you need to talk to, this can be a lot to remember. So, the ADSI feature is nice in that Flash worries about it for you.

Flex has no such feature.  When you bring in Flash content, you’ll get an exception, and you can’t debug it easily because it was the MovieClip’s timeline who caused the error. When you go to debug, you’ll get the 1 line exception “ReferenceError: Error #1056: Cannot create property” in the call stack, and nothing more… and you’re left wondering, who the heck is creating this thing?

While I agree why Adobe put the ADSI feature in Flash CS3, it works against you when you create content for Flex. Take the pain of writing out all of your MovieClip’s as class member properties, and uncheck ADSI via:

  1. File > Publish Settings
  2. Flash tab
  3. Settings button
  4. un-check “Automatically declare stage instances”

Good to go funky coma-deena.

Here’s the settings screen:

Settings Screen

Here you can see a new animation starts on frame 25. Therefore, if there is no class variable called “box”, you’ll get a runtime exception. In Flash CS3, however, you won’t if you have ADSI checked.

Timeline Animation Example

Here, you can see I define the box on line 8; this prevents a runtime exception when you embed the SWF in Flex.

Class Example

6 Replies to “Flash & Flex Integration Gotcha: Automatically Declare Stage Instances”

Comments are closed.