Loading a SWF in Flex and with a FlashVar alternative
This blog is about, how to take a SWF that requires the use of FlashVars and integrate it into Flex through a Loader.
Download code example here
Old method:
[html]
codebase="http://download.macromedia.com/pub/shockwave/cabs/flash
/swflash.cab#version=7,0,0,0" width="675"
height="450" id="cover" align="middle"> value="xmlFile=example.xml&progressColor=0x3333FF" /> flashvars="xmlFile=example.xml&progressColor=0x3333FF"
scale="noscale" bgcolor="#000000"
type="application/x-shockwave-flash"
width="675" height="450"
pluginspage="http://www.macromedia.com/go/getflashplayer" />
[/html]
To load SWF’s in Flash MX or Flex you can use the mx.controls.Loader class. In Flex there is a
You would simply use the code below in a Flex mxml file:
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml"
xmlns="*" width="100%" height="100%">
<mx:Script>
<![CDATA[
function setVars( event ) {
event.target.content.xmlFile = "cover.xml";
event.target.content.progressColor = "0xCCCCCC";
}
]]>
</mx:Script>
<mx:Panel width="400" height="200"
title="SWF Loader Flash Vars Test">
<mx:Loader id="slideshow" source="slideshow.swf"
complete="setVars( event )"/>
</mx:Panel>
</mx:Application>
If you have control over the original SWF’s fla you can make more complex ways for the Loader and the SWF to communicate. You can call functions and set variables from either the Loader or the loaded SWF.
The disclaimer about this blog is that depending how the SWF is created and what its functionality is you could have situations where the variables are not set at the correct time. So this doesn’t really work exactly like FlashVars but gives you capability to set up communication from a SWF and the Flex application loading it.