Loading a SWF in Flex and with a FlashVar alternative

August 30th, 2005

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:
  1. <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"
  2. codebase="http://download.macromedia.com/pub/shockwave/cabs/flash
  3. /swflash.cab#version=7,0,0,0" width="675"
  4. height="450" id="cover" align="middle">
  5.   <param name="FlashVars"
  6. value="xmlFile=example.xml&progressColor=0x3333FF" />
  7.   <param name="movie" value="slideshow.swf" />
  8.   <param name="quality" value="best" />
  9.   <param name="scale" value="scale" />
  10.   <param name="bgcolor" value="#000000" />
  11.   <embed src="slideshow.swf" id="cover" quality="best"
  12. flashvars="xmlFile=example.xml&progressColor=0x3333FF"
  13. scale="noscale" bgcolor="#000000"
  14. type="application/x-shockwave-flash"
  15. width="675" height="450"
  16. pluginspage="http://www.macromedia.com/go/getflashplayer" />
  17. </object>

To load SWF's in Flash MX or Flex you can use the mx.controls.Loader class. In Flex there is a tag.

You would simply use the code below in a Flex mxml file:

XML:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml"
  3. xmlns="*" width="100%" height="100%">
  4.    
  5.     <mx:Script>
  6.     <![CDATA[
  7.     function setVars( event ) {
  8.         event.target.content.xmlFile = "cover.xml";
  9.        event.target.content.progressColor = "0xCCCCCC";
  10.     }
  11.     ]]> 
  12.     </mx:Script>
  13.    
  14.     <mx:Panel width="400" height="200"
  15.                 title="SWF Loader Flash Vars Test">
  16.         <mx:Loader id="slideshow" source="slideshow.swf"
  17.             complete="setVars( event )"/>
  18.     </mx:Panel>
  19.    
  20. </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.

Entry Filed under: Adobe Flex

1 Comment

  • 1. Apple  |  2006-05-30 at 1.51 pm

    How would you call a method on the loaded SWF (and vice versa)?


Disclaimer: I work as a Flash/Flex Developer for Adobe Systems Incorporated. The opinions expressed here represent my own and not those of my employer.

My Amazon.com Wish List

Calendar

August 2005
S M T W T F S
    Dec »
 123456
78910111213
14151617181920
21222324252627
28293031  

Most Recent Posts


Flex.org - The Directory for Flex