Flash AS3 Tutorial – FlashVars – What is flashvars?

flash-logoWhat is FlashVars?

FlashVars is a way to pass data or variables from html to a Flash movie.  The general idea of FlashVars is very similar to a Query String.   Variables passed via FlashVars will go into the _root level of your Flash movie.

  • The data to be passed is fairly simple. Which could be simple text (such as user name, filename, cookie value or user id).
  • You should not implement Flashvars for sensitive or private data.  Anyone can easily see the content of the FlashVars by page view-source.  Something like a password or SSN should NEVER be passed via FlashVars.
  • FlashVars file size limit is 64K.

Sample Code AS3 : FlashVars

As shown below you can use the new LoaderInfo class object to access the FlashVars.  The FlashVars is available in the parameters member of the LoaderInfo.

In our example below, consider that you have different images that you want to load in your flash movie. The image should load depends upon which page the .SWF is loaded.

Assuming that the image filename is image1.jpg, and that it is located at a folder named images.  And assuming that the Flash movie will use a variable named imageFilename to refer to the file.

<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
 WIDTH="250" HEIGHT="250" id="flaMovie1as3" ALIGN="CENTER">
 <PARAM NAME=movie VALUE="flaMovie1as3.swf">
 <PARAM NAME=FlashVars VALUE="imageFilename=images%2Fimage1%2Ejpg">
 <PARAM NAME=quality VALUE=high>
 <embed src="flaMovie1as3.swf" FlashVars="imageFilename=images%2Fimage1%2Ejpg"
  quality="high" bgcolor="#FFFFFF" WIDTH="250" HEIGHT="250"
  NAME="flaMovie1as3" ALIGN TYPE="application/x-shockwave-flash"

In the example above, FlashVars contains one variable: imageFilename, with the value of “images/image1.jpg”.

In AS3, the FlashVars are no longer accessible in _root.  Instead, you need to use LoaderInfo to access them.

AS3 Code :

// This function will be called when the swf finished loading.
//  MovieClip "mc" is on the stage, this rootComplete() function will load
// imageFilename specified in the FlashVars.
function loaderComplete(myEvent:Event)
  var flashVars=this.root.loaderInfo.parameters;
  var loader = new Loader();
  loader.load(new URLRequest(flashVars.imageFilename));

// This assigns the callback to be called when the movie has finished loading.
this.loaderInfo.addEventListener(Event.COMPLETE, loaderComplete);

Event.COMPLETE method is used to make sure that SWF loads fully before FlashVars are being returned.

Hope this helps understanding basic concept of FlashVars in AS3. Drop me a comment if you liked this article and have any comments or feedback!