Using FlashVars with ActionScript 3.0

I was building a small app in Flash/ActionScript 3.0 over the weekend and needed to find out how to use FlashVars (Using FlashVars to pass variables to a SWF) to pass variables to my SWF file.

Previously, in ActionScript 2.0, you could pass variables along the query string or using FlashVars and the variables would be available in _level0, in ActionScript 3.0, it is a bit tricker:

<!-- using query string -->
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"
codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0"
width="550"
height="400"
align="middle"
id="main">
<param name="allowScriptAccess" value="sameDomain" />
<param name="movie" value="main.swf?one=1&two=2" />
<param name="quality" value="high" />
<param name="bgcolor" value="#ffffff" />
<embed src="main.swf?one=1&two=2"
width="550"
height="400"
autostart="false"
quality="high"
bgcolor="#ffffff"
name="main"
align="middle"
allowScriptAccess="sameDomain"
type="application/x-shockwave-flash"
pluginspage="http://www.macromedia.com/go/getflashplayer" />
</object>

The previous code passes two variables (one and two) along the querystring (main.swf?one=1&two=2) and makes those variables available in your SWF file in the root’s loaderInfo object (or more accurately, within the loaderInfo’s parameters property).

Also, the same technique works if you pass the variables within a FlashVars parameter, as seen below:

&lt!-- using FlashVars -->
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"
codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0"
width="550"
height="400"
align="middle"
id="main">
<param name="allowScriptAccess" value="sameDomain" />
<param name="movie" value="main.swf" />
<param name="quality" value="high" />
<param name="bgcolor" value="#ffffff" />
<param name="FlashVars" value="one=1&two=2" />
<embed src="main.swf"
width="550"
height="400"
autostart="false"
quality="high"
bgcolor="#ffffff"
FlashVars="one=1&two=2"
name="main"
align="middle"
allowScriptAccess="sameDomain"
type="application/x-shockwave-flash"
pluginspage="http://www.macromedia.com/go/getflashplayer" />
</object>

But enough of my rambling, how do you access these from within your FLA document? Read on!

// AS3
var tf:TextField = new TextField();
tf.autoSize = TextFieldAutoSize.LEFT;
tf.border = true;
addChild(tf);

tf.appendText("params:" + "\n");
try {
var keyStr:String;
var valueStr:String;
var paramObj:Object = LoaderInfo(this.root.loaderInfo).parameters;
for (keyStr in paramObj) {
valueStr = String(paramObj[keyStr]);
tf.appendText("\t" + keyStr + ":\t" + valueStr + "\n");
}
} catch (error:Error) {
tf.appendText(error.toString());
}

The previous code creates a new TextField instance on the Stage (named tf), uses a for..in loop to iterate over each of the passed parameters and displays the parameter’s names and values in the text field.

Nothing ground breaking or overly fascinating, but hopefully this helps somebody out there or saves them about 3 minutes of searching.