Moving From Flash AS2 to Flash AS3: FlashVars

What is FlashVars?

FlashVars is Flash answer to Query String.  It’s a way to pass data or variables from html to a Flash movie.  Variables passed via FlashVars will go into the _root level of the Flash movie.

Differences Between AS2 and AS3

The key difference between AS2 and AS3 is how to retrieve the FlashVars. The FlashVars are no longer available in the _root level of the movie. Instead, you must use the new LoaderInfo class object to access the FlashVars.  The FlashVars is available in the parameters member of the LoaderInfo. This makes the AS3 solution much more dynamic and independent to each object that is created. See below for an example of both.

FlashVars in AS2

 

var myFlashVar:String;
_root.createTextField("myTextField", 1, Stage.width/2 - 50, Stage.height/2 , 150, 40);
myTextField.text = myFlashVar;

FlashVars in AS3

 

function loaderComplete(myEvent:Event)
{
this.flashVars=this.loaderInfo.parameters;
this.flashVarsLoaded=true; // set a flag to indicate that the FlashVars is loaded
}


I hope this post helped you understand the key difference between defining your FlashVars within AS2 and AS3.  If your interested in learning more about FlashVars, check out the resources below as they will go deeper into the topic.

Resources on FlashVars:

I would like to hear from you on how I can make the series of posts “Moving From Flash AS2 to Flash AS3” better.  Looking forward to hearing from you!

3 thoughts on “Moving From Flash AS2 to Flash AS3: FlashVars

  1. For anyone looking for a quick example, be aware that there is no flashVarsLoaded property and there is no useFlashVars() method. I’m not sure if the intention was to show more code, but this is confusing at best. It makes it look as though the difference between AS2 and AS3 is so much deeper than it is. Instead of accessing a variable on _level0, you access a property on root.loaderInfo.parameters (AS-only or Flash projects) or Application.application.parameters (Flex projects).

    • @Derek: I did not even notice that extra bit of code. My apologies. I am surprised that no one mentioned it earlier. Thanks for the heads up!

Comments are closed.