Flash: FlashVars in AS3
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:
1 2 3 | var myFlashVar:String; _root.createTextField("myTextField", 1, Stage.width/2 - 50, Stage.height/2 , 150, 40); myTextField.text = myFlashVar; |
...
1 2 3 4 5 6 7 8 9 | <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"> codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="200" height="150" id="flashVar_AS2" align="middle"><embed src="flashVar_AS2.swf" quality="high" bgcolor="#ffffff" height="150" width="200"></embed> name="flashVar_AS2" flashVars="myFlashVar=THIS IS MY TEXT" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />; </object> |
...
1 | <param name="FlashVars" value="myFlashVar=THIS IS MY TEXT"></param> |
and here:
1 | 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...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | //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



April 26th, 2008 - 10:07
http://video2.channelnewsasia.com/cnavideos/chineseplayer.asp?skin=Player1.swf&player=chineseplayer.swf&filename=xin_042308_fannwong2.flv
hi, is there a way to download the above flash video?
cos whenever i tried to download its just the player skin and not the video.
or what other information do i need to be able to download?
May 20th, 2008 - 08:53
Abobe code AS3 – is not working, will u explain in detail about this.
July 8th, 2008 - 22:20
Finally! A FlashVars tutorial that’s easy to follow and that ACTUALLY WORKS straight from this page.
Thanks a lot.
July 14th, 2008 - 21:18
Hi there, thanks for the great tuturial. How would I apply this to streaming video playlist? I have it set up dynamically and can include any necessary parameters in the XML for each vid but I don’t know how to tie it all together. Any help you can give I would really appreciate. Thanks!
September 16th, 2008 - 14:28
Hello, im Lucas from Argentina. Sorry for my poor english!!
I have a problem with FlashVars. In this example you put the text in the parameter (“myFlashVar=THIS IS MY TEXT”). How can I do if I want to get the text of the parameters from a textfield in the html page instead of write it with the keyboard?
I mean to make something like:
param name=”FlashVars” value=”myFlashVar=” + txtPar.value
I have tried this, but i get an error. Can you help me?
Thanks a lot! And sorry for the mistakes in the text!
February 6th, 2009 - 09:32
Thanks a lot.
February 26th, 2009 - 04:45
1. Please visit http://www.busycode.com or http://www.flexdeveloper.com.cn
2. Low cost high quality.
3. Best Adobe Flex outsourcing service provider.
4. More than 60 full time in-house Flex developers.
5. Our company Busycode Inc. was registered in San Francisco, Beijing and Nanning.
6. Our skill set is “Adobe Flex/AIR/Flash + .NET/Java/PHP + SQL Database”.
7. Our keywords are Flex developer, Flex coder, Flex programmer, Flex expert, Flex engineer, Flex specialist and Flex outsourcing service provider.
March 7th, 2009 - 03:20
Thank you for this tutorial. It is helpful, I have one question though:
when my text is displayed in the .swf it has a font face of time new roman. How can I change this font, to say arial of veranda?
March 18th, 2009 - 18:48
To change the font to one like arial, you use the textFormat object and set the properties on it.
For example
var tf:TextFormat = new TextFormat();
tf.font = "Arial";
textbox.setTextFormat(tf);
textbox.embedFonts(true);
March 24th, 2009 - 08:07
it is great to see the toturial.Before i was much worry about then flashvars .now it’s seem easy to me.
March 24th, 2009 - 08:09
May 28th, 2009 - 22:19
I hate using tutorials that don’t seem to work straight from the code but some people say it does
May 28th, 2009 - 22:23
I’m wrong, it works. I’m eating crow
May 28th, 2009 - 22:46
My last post no matter what. It does and it doesn’t work. When I put this code in I can get text to display in the HTML but in Flash I get:
TypeError: Error #2007: Parameter text must be non-null.
at flash.text::TextField/set text()
at promotions_fla::MainTimeline/promotions_fla::frame1()
June 4th, 2009 - 22:33
Few more ways to pass variable to flex
http://yasob.blogspot.com/2009/05/accessing-http-request-parameter-in.html
July 4th, 2009 - 12:02
“don’t pass sensitive data using this method as everybody can see what you passing by doing a right click and view source”
so what do i do if i need to hide the data?
July 28th, 2009 - 23:29
Example did not work for me, same error as Matt:
TypeError: Error #2007: Parameter text must be non-null.
at flash.text::TextField/set text()
at promotions_fla::MainTimeline/promotions_fla::frame1()
August 4th, 2009 - 21:20
@Matt
you need to give your text field an instance name
September 24th, 2009 - 17:39
@michelle
wow u are an idiot
September 24th, 2009 - 17:42
im getting the same error as Gordon and Matt, it says must be non-null.
what does this mean???
i HAVE set an instance name for my text field, and then I inserted that into the code in place of “myTextField”
can anyone paste the code the way they did it with their instance names? or describe how this is donw??
September 24th, 2009 - 20:05
u r dumb
October 9th, 2009 - 22:36
Dude, you overcomplicated something that’s as simple as:
//AS3
var paramObj:Object = this.root.loaderInfo.parameters;
var myFlashVar:String = parameters.myFlashVar;
//or this (still AS3)
var myFlashVar:String = this.root.loaderInfo.parameters.myFlashVars;
October 9th, 2009 - 22:36
Dude, you overcomplicated something that’s as simple as:
//AS3
var paramObj:Object = this.root.loaderInfo.parameters;
var myFlashVar:String = paramObj.myFlashVar;
//or this (still AS3)
var myFlashVar:String = this.root.loaderInfo.parameters.myFlashVars;
October 18th, 2009 - 01:51
if u dont know how to do it also dumb la, dont say people dumb
November 18th, 2009 - 15:54
thx very much
i’ve met a lot of problem in as3 since read this
December 3rd, 2009 - 15:13
Thank you Clay, for pointing it out so simple. Works like a charm!
December 3rd, 2009 - 21:36
Brother! You saved my life!
It’s the better exemple that I found and all World Wide Web!
Thanks a lot!