Flex to Flash Communication
February 18th, 2009To celebrate the launch of The NAACP Top 100, I thought I would do a brief tutorial on how to communicate from Flex to Flash.
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.
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:
myFlashChild = flashLoader.content as MovieClip;
}
flashLoader is just a SWFLoader used to load in the external swf that you created with Flash. The MXML for the SWFLoader looks like:
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:
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:
myFlashChild.setTextFromFlex(flexTxt.text)
}
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:
readOutTxt.text = myString;
}
That’s it. Let me know if you have any questions. Class is dismissed.







![Validate my RSS feed [Valid RSS]](http://www.flashinyourface.com/wp-content/images/valid-rss.png)
![MXNA MXNA]](http://www.flashinyourface.com/wp-content/images/mxna88x31_grey.gif)
