Flash: FlashVars in AS3

!!!!UPDATE!!!!!!
I’ve wrote a quick cookbook recipe on how to handle flashvars, published on Adobe’s Cookbook
Here the URL to the recipe: Reusable flashvars class http://bit.ly/d3eUpl

————————————————————————-

Everybody knows that FlashVars are pretty useful when you want to work with dynamic data being passed from the URL or from the object tags, that gives you a bit of a dynamic touch to your application as well allows you to shorten out your delivery time since you can create your flash object only once, for example you want to publish 100′s of videos like youtube does for example, but you don’t want to create for every clip a single swf.. Believe me there are people out there that are still doing that. If you don’t have a big ass database like most web 2.0 sites have, then flash vars is the intermediate solution.

Actionscript 2.0:
While we’re still scripting AS 2.0 in flash 8, we could just define an empty variable in our first frame, and the values being passed from the URL or through the Embed object were passed to our flash movie.

In the example below I’ve created a swf that it’s getting the content of it’s textfield through FlashVars using Actionscript 2.0
Example:

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

<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="100" height="100" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="src" value="flashVar_AS2.swf" /><param name="bgcolor" value="#ffffff" /><param name="quality" value="high" /><embed type="application/x-shockwave-flash" width="100" height="100" src="flashVar_AS2.swf" quality="high" bgcolor="#ffffff"></embed></object>

and here:

flashVars="myFlashVar=THIS IS MY TEXT"

You just need to add the flashVars parameter to your object & Embed tag and you are ready to go, don’t forget to create a new variable in you AS project otherwise there might be errors during FlashVars are being processed.

Actionscript 3.0
With actionscript 3 a few things changed, it’s not enough just creating a variable, you need to tell flash to go pickup the flashvars otherwise nothing will happen, how you do that? Read on…

//FLASHVARS CODE
var varName:String;
var paramObj:Object = LoaderInfo(this.root.loaderInfo).parameters;
for (varName in paramObj) {
	myFlashVar = String(paramObj[varName]);
}
 
//INIT VARIABLES
var myFlashVar:String;
var myTextField:TextField;
 
//TEXTFIELD PROPERTIES
myTextField = new TextField();
myTextField.text = myFlashVar;
myTextField.x = stage.width / 2;
myTextField.y = stage.height / 2;
addChild(myTextField);

As on the previous example we create a new textfield assign the content of it to our FlashVars variable and add it to the stage with the addChild method.
The FlashVars code (commented) is the only piece you need to do to be able to read out the values of it.
First of all we define varName which will be the showing us the name of the variable, paramObj is going to be our variable content
During the for loop we pass our flashVars to the appropriate variable, if you were passing multiple variables you would just need to rewrite the for loop and assign the flashvar to each variable.

For the HTML you just need to add the exact same code as we did before for our AS 2.0 example.

I hope that people understand how useful Flashvars really are for your daily work, just keep something in mind which is very important, don’t pass sensitive data using this method as everybody can see what you passing by doing a right click and view source.

Tiago

Leave a comment

56 Comments.

  1. I have a file. swf download from a website. But it can attach some data from flashvar properties. so file. swf does not work. Can you help me ?

Leave a Reply

Your email address will not be published.


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Trackbacks and Pingbacks: