AS2 → AS3: Retrieving FlashVars

Download Example Files

I was recently working on the Image Slider that I posted here a couple of days ago and I realized I didn't know how to access FlashVars in AS3. I quickly did a Google search and came up with the solution rather easily.

Below is the code for the brand new SWFObject 2.0 way to embed an SWF:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "">
  2. <html xmlns="" lang="en" xml:lang="en">
  4.     <head>
  5.         <title>FlashVars Test</title>
  7.         <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  9.         <script type="text/javascript" src="scripts/swfobject.js"></script>
  10.         <script type="text/javascript">
  11.             swfobject.embedSWF("swf/AS3.swf", "flashcontent", "550", "400", "9.0.0", false, {param1: "parameter one value", param2: "parameter two value"}, {menu: "false"}, {id: "flashID", name: "flashvars-test"});
  12.         </script>
  14.         <link REL="stylesheet" TYPE="text/css" HREF="scripts/styles.css" TITLE="styles">
  16.     </head>
  18.     <body>
  20.         <div id="flashcontent">
  21.             <strong>You need to upgrade your Flash Player</strong><br /><br />
  22.             This site uses Flash Player 9 or above to display its content. Please download the latest version <a href="" target="_blank">here</a>.<br /><br /> 
  23.             If you have Javascript disabled, please enable it now.
  24.         </div>
  26.     </body>
  28. </html>

As you can see, the embedSWF method gets passed in a bunch of parameters. Among those, you can pass an object that is your FlashVars, in this case "param1" and "param2". These are the values we want to grab out of the FlashVars and get inside of our FLA. The code to do this in AS2 used _level0 (or _root), which is no longer available in AS3.

  1. info_txt.text = _level0.param1;
  2. info_txt.text += "\n" + _level0.param2;

In AS3 you use the loaderInfo property of a DisplayObject, in this case our main stage. The loaderInfo property is part of the LoaderInfo class which in turn has a parameters property which allows you to access the FlashVars we discussed earlier.

  1. info_txt.text = this.loaderInfo.parameters.param1;
  2. info_txt.text += "\n" + this.loaderInfo.parameters.param2;

This is a pretty simple approach and very easy to do. It isn't as short as the AS2 version (which seems to be the case with many of these conversion basics), but it makes a lot more sense than using levels and such.

If you found this post useful, please consider leaving a comment, subscribing to the feed, or making a small donation.


[...] A brief article on this can be found at: [...]

Tnx. Saved my day. 🙂

WHat if I have many variables to pass on to another swf, how should it be coded? thanks!

@Yuklid: You can pass as many parameters to the flashvars as you'd like.

If you want to have a reusable function with a default value in case the user didn't pass that parameter to your flash file, try this post for a sample:


@Martin: Yep, this is an old post and since then I've made a static method that I use in a utility class that handles this. It's similar to yours except I have to pass in the display object to use to get the flash vars because its in a separate class:

  1. /**
  2. * Returns the specified FlashVar.  If one does not exist, it returns the default value provided.
  3. *
  4. * @param $do The DisplayObject to pull the loaderInfo from.
  5. * @param $var The variable to pull.
  6. * @param $default The default value to use if the variable is not found (optional).
  7. *
  8. * @return String
  9. */
  10. public static function getFlashVar($do:DisplayObject, $var:String, $default:String = ""):String
  11. {
  12.     var str:String = ($do.loaderInfo.parameters[$var]) ? $do.loaderInfo.parameters[$var] : str = $default;
  14.     return str;
  15. }

So to use it you would do this:

  1. DataUtils.getFlashVar(this, "test", "defaultName");

Como faço pra chamar uma variavel dentro do PHP


Leave a comment