Accessing SWFs from Flex and Vice-Versa

I’ve seen a number of questions regarding the interaction between a SWF and Flex. The idea is that you create some Flash content, perhaps a navigation bar, and you want to have events that happen within the SWF processed by the Flex app in which the SWF is embedded. Or you have the reverse: controls in the Flex app modify the behavior of an embedded SWF.

Both types of interactions are possible. You will be able to cook up more elaborate and elegant solutions, but this should get you started.

Download samples


The sample file contains a Flash document and Flex application. The Flash document contains a spinning ball and a few functions. The Flex application controls the color of the ball and can pause or resume its spin. The ball in the Flash document is covered by an invisible button which when click, invokes a method in the Flex application.

The key here is the Flex Loader’s content property. Once the SWF has loaded and the Loader fires its “complete” event, the content property is the SWF.

You can pass a reference to the Flex application (or any component) to the SWF in the complete event handler:

<mx:Loader source=”Ball.swf” complete=”event.target.content.flexApp=this” />

Within the SWF, you can now reference “flexApp” and invoke the “onBallClick” function.

Once the SWF has been loaded into the Flex application, you can invoke methods or set values in the SWF from the Flex application. For example, when you press the Pause (Flex) button, the SWF’s pause() method is invoked:

loader.content.pause();

Use the sample files as a guide to building your own hybrid applications.

Tip: Try not to use any of the Halo (or V2) components in your Flash document. The Flex components and Flex components while similar are not 100% compatable. Besides, why would you use buttons, labels, and datagrids in Flash when you have Flex?!?