Categories
Articles

Flex LiveConnectionand Legacy Flash Swfs

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.

[cc lang=”actionscript” tab_size=”3″ line_numbers = “true”]
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” );

[/cc]
Here is the SWFLoader component:


xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="initSwfComp()">