Flex LiveConnection and Legacy Flash SWFs
Posted by Lon Hosford on March 13th, 2008 at 08:05pm
The more RIA shops move web apps into Flex the ugly issue of integration of legacy Flash SWFs into Flex may show up.
The likely Flex component to use for loading Flash SWFs is SWFLoader. It can handle Actionscript 2 legacy Flash 8 or CS3 SWFs.
You may also use SWFLoader for cases of separate independent Flash SWFs that also need to be used into a Flex app and you may not have control over the SWF internal development requiring preparedness for just about anything.
For example you get SWFs with independent inputs from places such as FlashVars, external XML or even another SWF container. Perhaps there are internal functions in place handling these tasks.
However you find that code you need to call from Flex is not available on frame 1. You may find SWFLoader cannot call those functions due to timing inside the Flash SWF and you do not want to recode the SWF.
Then you might find using LocalConnection as a solution. You have the SWF invoke functions in the Flex SWFLoader component to inform ready states such as code available on a certain frame. Then the Flex function can use LocalConnection to call the SWF function.
Here is an example where the requisite code was on frame 10 of the FLA file.
- System.security.allowDomain("*");
- var _fromFlex_lc:LocalConnection = new LocalConnection();
- _fromFlex_lc.allowDomain("*");
- _fromFlex_lc.loadXML = function(xmlStr:String):Void
- {
- Main.getInst().loadXML ( xmlStr );
- _fromFlex_lc.close();
- }
- _fromFlex_lc.connect("flex2Swf_lc");
- var _outgoing_lc:LocalConnection = new LocalConnection();
- _outgoing_lc.allowDomain("*");
- _outgoing_lc.send( "swf2Flex_lc", "swfReadyToLoad" );
Here is the SWFLoader component:
- <mx:swfloader source="pathToSwf/the.swf" width="550" height="400">
- xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="initSwfComp()">
- <mx:script>
- <!--[CDATA[
- private var fromSwf_lc:LocalConnection;
- private var toSwf_lc:LocalConnection;
- private var swfXmlData:XML;
- public function set dataProvider(xmlData:XML):void
- {
- swfXmlData = xml;
- }
- private function initSwfComp():void
- {
- Security.allowDomain("*"); // If needed for internal SWF
- createLocalConnection();
- }
- private function createLocalConnection():void
- {
- toSwf_lc = new LocalConnection();
- toSwf_lc.allowDomain("*");
- fromSwf_lc = new LocalConnection();
- fromSwf_lc.allowDomain("*");
- fromSwf_lc.client = this;
- fromSwf_lc.connect( "swf2Flex_lc" );
- }
- public function swfReadyToLoad() : void
- {
- sendXMLData();
- }
- private function sendXMLData():void
- {
- toSwf_lc.send( "flex2Swf_lc", "loadXMLData", swfXmlData.toXMLString());
- fromSwf_lc.close();
- }
- ]]-->
- </mx:script>
- </mx:swfloader>
1 Comment for Flex LiveConnection and Legacy Flash SWFs
1. » Flex LocalConnect&hellip | March 13th, 2008 at 10:50 pm
[…] To see sample code to overcome this look at my post on using Flex, SWFLoader and LocalConnection: Flex LiveConnection and Legacy Flash SWFs […]
Leave a Comment for Flex LiveConnection and Legacy Flash SWFs
You must be logged in to post a comment.
Trackback this post | Subscribe to the comments via RSS Feed