PDA

View Full Version : Passing FlashVars via SwfLoader


tsj4
07-03-2008, 08:57 PM
I am loading a swf which is expecting some defined params using the swfloader into a flex application.

When attempting to append the vars to the end of the swf content source as I would do in the html embed tag Flex errors. It does not error of the ampersand but on the equals sign after the var name.

I do not see a property of the swfloader which refers to flashvars nor when I define the var in my flex app is it noticed by the loaded swf.

Is their a way to pass in values into a swf which gets loaded in through the swfLoader?

Sly_cardinal
07-04-2008, 04:37 AM
Are you able to post a code sample?

I was just looking into whether it is possible to pass flashvars through the SWFLoader component and I found that it was indeed possible.

tsj4
07-08-2008, 12:47 AM
Flex 3 - Code Snippet
- 'us.swf' and 'senate.xml' both live in the folder 'map' which is in 'src'.
- 'us.swf' is looking for the flashVar 'data_file'.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import flash.events.*;

private function setVars(event):void
{

event.target.content.data_file = "/map/senate.xml";
}
]]>
</mx:Script>
<mx:SWFLoader width="300" height="100" source="map/us.swf" complete="setVars(event)" />
</mx:Application>

Sly_cardinal
07-08-2008, 01:22 AM
I've had success previously appending the flashvars to the source URL:


<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">

<mx:Script>
<![CDATA[
import flash.events.*;
import mx.events.FlexEvent;

override protected function childrenCreated():void
{
super.childrenCreated();
mapLoader.source = "map/us.swf?data_file=/map/senate.xml";
}
]]>
</mx:Script>

<mx:SWFLoader id="mapLoader" width="300" height="100" />

</mx:Application>

tsj4
07-08-2008, 01:34 AM
you got it. Thanks for the code and the speedy response. How did you know to extend the childrenCreated function? I do not see it mention on adobe docs for SWFLoader for flex 3.

Also why does flex treat this differently then just adding that string as the source like so?


<mx:SWFLoader source = "map/us.swf?data_file=map/senate.xml" id="mapLoader" width="300" height="100" />


this was my first attempt

Sly_cardinal
07-08-2008, 01:49 AM
Actually, it shouldn't treat that any differently:


<mx:SWFLoader source = "map/us.swf?data_file=map/senate.xml" id="mapLoader" width="300" height="100" />


I just wasn't sure if the url variables would cause issues with the mxml compiler. I've just tested it and it was fine, so you shouldn't have any problem defining it in that way.

With regard to the childrenCreated function: that is a fundamental part of the Flex framework. If you look at the class definition for the UIComponent class (http://livedocs.adobe.com/flex/3/langref/mx/core/UIComponent.html) you can see that it defines a whole suite of properties, methods and events. These methods and events allow developers to hook into the framework to get things done.

More info on the flex component lifecycle here:
http://www.onflex.org/ted/2007/02/flex-instantiation-life-cycle-and-event.php

DrElvisToad
08-30-2008, 01:28 AM
I'm having the same problem. I've done this:

<mx:SWFLoader source = "map/us.swf?data_file=map/senate.xml" id="mapLoader" width="300" height="100" />

and that runs with no errors but the loaded SWF "us.swf" is getting null for data_file when I use this.parameters.data_file or Application.application.parameters.data_file