Normally Flex will strip all ActionScript from an embedded Flash symbol. We've developed a technique called Shake'n'Bake SWFs (yes, I did watch Talladega Nights before coming up with the name), which allows you to utilize Flash symbols in Flex projects with all AS intact. I mentioned this technique at 360Flex, but didn't go into much detail because there are better options available when using Flash CS3. A couple of people mentioned that they would like to learn about the technique, so I thought I'd blog about it.

Shake'n'Bake SWFs are relatively easy to implement. First, move all of the symbols you want to use into a new FLA in Flash 9 Public Alpha (or CS3), and publish a SWF - ensure the symbols have a Class name, and that they are set to export in the first frame. Next, embed the SWF in your Flex project as binary data.
[Embed(source="mySWF.swf", mimeType="application/octet-stream")]
var SWFBytes:Class;
Next, you need to instantiate the embedded SWF by using Loader.loadBytes. You could do this in an initialization handler, for instance.
var loader:Loader = new Loader();
loader.loadBytes(new SWFBytes());
This will reconstitute your SWF. Now you just need to reach into the SWF to pull out symbols or classes. Unfortunately, the reconstituted SWF is not immediately available. You will have to wait one frame or until the loader's contentLoaderInfo dispatches it's INIT event. Once it's inited, you can use the loader's contentLoaderInfo.applicationDomain.getDefinition method to pull out classes or symbols you'd like to use.
var SymbolClass:Class = loader.contentLoaderInfo.applicationDomain.getDefinition("com.gskinner.MyClass");
var symbolInstance:Sprite = new SymbolClass() as Sprite;
Pretty straightforward, but we've also built a class called FlashLib that handles most of the details for you, and a jsfl script that will set up and update the class for you with one click. Once you've downloaded / installed the MXP and restarted Flash, you will find two new commands in your Commands menu. "FlashLib - Create" will prompt you for your project location, then generate the SWF and a FlashLib.as file in the project directory. This command will also store the project directory, so that you don't have to find it again on subsequent runs. If you ever want to change the saved project locations, you can use the "FlashLib - reset info" command to clear the saved location.

The FlashLib class that is generated will handle all of the Shake'n'Bake details for you. Simply add a listener for init, or call init() on FlashLib, and wait one frame (it takes one frame for assets to be available after the class is first accessed).
// because it uses static (class level) accessors, we need to add the listener to an eventDispatcher property:
FlashLib.eventDispatcher.addEventListener(Event.INIT,handleFlashLibInit);
You can verify that the symbols and classes are available by checking the init property at any time:
if (!FlashLib.inited) {
     FlashLib.init();
}
Once it is inited, you can use the getDefinition and getInstance methods to get class definitions, and instances thereof from the Flash SWF. You can then use DisplayObjectWrapper to insert it into a Flex container.
function handleFlashLibInit(evt:Event):void {
    // get a symbol instance from the SWF:
    var myFlashSymbol:MovieClip = FlashLib.getInstance("MySymbol") as MovieClip;
    // wrap it so that it will work in Flex layouts:
    var myDOW:DisplayObjectWrapper = new DisplayObjectWrapper(myFlashSymbol);
    // add it to the Flex container:
    myFlexContainer.addChild(myDOW);
}
Considerations when using this technique:
  • You cannot (easily) strong-type your symbol instances, you will need to type them as a native object type (ex. DisplayObject, Sprite, MovieClip)
  • Symbols cannot be added to Flex layout controls without first being wrapped in IUIComponent friendly class (see my blog post on DisplayObjectWrapper for more details)
  • This technique will only work with AS3 content (from Flash 9 Public Alpha, Flash CS3, or Flex 2)
You can download the MXP here (includes the FlashLib classes and Flash commands). If you're just interested in the FlashLib class, you can download it separately here. I'll try to put up a simple demo in the next few little as well. To learn about and download the DisplayObjectWrapper class, view this entry.