Using FlashVars to pass variable to your SWF.

FlashVars are a great way to pass information to your swf. The most common usage is to pass along the root path to use when looking up resources or configuration information. These parameters can then be accessed within your application by the use of ActionScript and are a great way to increase the customization and re-usability of your code.

To start things off lets look at a few possible ways to pass FlashVars into your SWF application. The first way is by use of the Object or EMBED tags. This is only for those developers still out there that haven’t started utilizing SWFObject to embed their SWFs into the page.

Object Tag Example

When using the Object tag you need to specify a param with the name FLashVars that has its values set to name=value pairs with an & separating the variables.


Embed Tag Example

To use FlashVars in an EMBED tag you simply define a FLASHVARS attribute with the values set the same as in the object tag.


Most Flash Developers do not however still utilize the the Object and Embed Tags to display SWFs instead they most often use JavaScript libraries like SWFObject that handle the code output.

SWFObject Example

With SWFObject you simply pass an associated array that contains named values into your embedSWF call, and the library will take care of outputting the correct code.

var flashvars = {baseURL: "www.mdbitz.com", mode: "single"};
		var params = {wmode: "transparent", allowFullScreen: "false"};
		var attributes = {};
		swfobject.embedSWF("mySWF.swf", "mySWF", "800", "200", "9.0.0", "flash/expressInstall.swf", flashvars, params, attributes);

Obtaining the FlashVars in ActionScript 3

Now that you are passing variables into your Flash application you need to obtain and use those values. In ActionScript 3 we need to get the parameters out of the root LoaderInfo object.

var paramObj:Object = null
try {
    paramObj = LoaderInfo(this.root.loaderInfo).parameters;
} catch (error:Error) {
    // Flash Vars could not be loaded
}

The parameters property of the LoaderInfo class returns an Object that can be treated similar to an associated array. All you need to do to get your named properties is:

var baseURL = paramObj["baseURL"];

That is all there is to using FlashVars. You will find that once you start using them it will greatly enhance the re-usability of your applications and also reduce deployment issues from path errors.

// Flash //

Comments & Questions

Add Your Comment