« Initialized | Main | Ticker »
December 16, 2004
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.
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?!?
Posted by pent at December 16, 2004 03:30 PM
Trackback Pings
TrackBack URL for this entry:
http://weblogs.macromedia.com/mtadmin/mt-tb.cgi/6243
Comments
<mx:VBox width="400" height="400">
<mx:Image id="swf" source="Flash/JustMovie.swf"/>
<mx:Button label="gotoAndStop" click="swf[0].gotoAndStop(9)"/>
<mx:HSlider width="320" id="slider" tickInterval="1" minimum="1" maximum="15" snapInterval="1" change="swf[0].gotoAndStop(slider.value)" liveDragging="true" />
</mx:VBox>
Posted by: Chris Phillips at December 16, 2004 05:54 PM
can u upload some examples for this?
Posted by: Eric Luo at December 18, 2004 08:33 AM
Oh! Too has been really good!
Chris Phillips's examples is very good too!
Posted by: hiphen(china) at December 20, 2004 10:59 PM
Ok - now just tell me how to load a Breeze Presentation SWF (that main.swf) into Flex and I owe you some beer :)
Posted by: Dirk Eismann at December 23, 2004 03:39 AM
What about Flash Paper 2 .swf? Tried to load it into Flex, didn't happen ... just a broken image icon showed after some delay...
Posted by: pemoke at January 19, 2005 12:39 AM
Thanks!
Posted by: Fred Knack at January 20, 2005 02:22 PM
You should be able to load a Flash Paper SWF using
Posted by: Peter Ent at January 21, 2005 05:09 PM
Could you please point to a sample for loading Flash Paper into Flex?
It loads fine into Flash with 2 lines of code, but with Flex I am experiencing problems - especialy with the size. I took sample from Flash Paper 2 home page and it will size to maximum of 300x150px. Tried absolute/relative sizes for and even wrapping in but it won't size beyond 300x150. Here is mine:
I've also noticed some inaccuracies in behaviours of some components in Flash players for Win MS IE vs. Mozilla. Is there a bug resource/listing somewhere available?
Posted by: pemoke at February 6, 2005 10:47 PM
code sample in re. prev. post was removed, so again here without tags:
mx:HBox width="100%" height="100%"
mx:Image source="frontdoor_doc.swf"
mx:HBox
Posted by: pemoke at February 6, 2005 10:49 PM