jump to navigation

Flex to Flash Communication July 24, 2010

Posted by Tournas Dimitrios in Flash, Flex.

Loading swf files inside Flex and communicating with them can be done through different strategies .

This is one approach that I like to share with you.In short, it’s not too big of a deal. You have to create a MovieClip object in your Flex movie that you will equate with the Flash movie that you load into the Flex. Once you have that object, you can call it’s methods, pass variables, and what not. The only catch is that you need access to the guts of the Flash movie so you know what objects and methods that it contains.The picture below demostrates the final result


So, in the Flex, create the MovieClip object that you will later construct:

private var myFlashChild:MovieClip;

Next, you will need a function to call that constructs your MovieClip object using your Flash swf:

private function init():void{
myFlashChild  = flashLoader.content as MovieClip;
//also this is valid :
//myFlashChild = MovieClip(flashLoader.content);

flashLoader is just a SWFLoader used to load in the external swf that you created with Flash. The MXML for the SWFLoader looks like:

<m x: S WFLoader y="50" source="assets/flash_comm.swf" horizontalCenter="0" complete="init()"  id="flashLoader" />

The init() method called is what plugs your external Flash swf into your Flex MovieClip object. The external swf needs to finish loading into the Flex before this method is called, which is why the method is called on complete.
Now that your external swf is loaded, you can call the methods that it contains and because we built the Flash, we know that it has a function in it called setTextFromFlex which looks like this:

function setTextFromFlex(myString:String):void{
readOutTxt.text = myString;

So finally from Flex, we can call our function which sends a string of text from our Flex textfield into the Flash function which will then display that text inside of the textfield that is in the Flash:

private function communicateToFlash():void{

So, the final block of code from Flex will look like this>>

For the Flash, besides naming your components, all you’ll need to do is add this function:

function setTextFromFlex(myString:String):void{
readOutTxt.text = myString;

Download the sourceZip here >>>


No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s