I’ve had a couple special requests to explain flashvars and how to use it and show it in action.
Overview
The property “FlashVars” can be used to import root level variables to the flash movie or swf. The flashvars propery is used in codes for embedding flash in the html page. The string of variables passed in as flashvars, will be imported into the top level of the movie when it is first instantiated. Variables are created before the first frame of the SWF is played. The format of the string is a set of name=value combinations separated by ampersand (&) symbols.
Steps
- Include the flashvars property in your embed codes and voila! You have these variables to use in your swf.
- That’s the one step
Code
HTML Embed Codes
1 2 3 4 5 6 | Here's some sample embed codes, including object and embed tags: <object width="540" height="240" title="sample"> <param name="movie" value="flashvarsTutorial.swf" /> <param name="flashvars" value="var1=here&var2=are&var3=my&var4=flashvars" /> <embed src="flashvarsTutorial.swf" flashvars="var1=here&var2=are&var3=my&var4=flashvars" type="application/x-shockwave-flash" width="540" height="240" ></embed> </object> |
Actionscript using flashvars
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | //flashvars="var1=val1&var2=val2&var3=val3"; display("var1 = "+ var1); display("var2 = "+ var2); display("var3 = "+ var3); display("var4 = "+ var4); function display(todisplay:String){ feedback.text += todisplay+"\n"; trace(todisplay); } |
Example
Page 1 (var1=val1&var2=val2&var3=val3)
Page 2 (var1=here&var2=are&var3=my&var4=flashvars)
Source
Download the html files and the fla and swf in this flashvars.zip
























15 Comments
Hey.
Nice example for start..!
You have managed your blog very well
Hello, I have the files and everything and i was testing around with the fla file and the html page so it will only show up var 1 and 2 but even tho when i take them out of the fla and html page var 3 and var 4 they still show up and say undefined on it.. help?
If you use CS3 and publish html, you need to add
In the AC_FL_RunContent code the following:
‘FlashVars’, ‘var1=val1&var2=val2&var3=val3′
Thanks. It worked. I never knew it is so easy. I was actually messing a lot with the ActionScript 3.0 although I was only looking for something very simple like the example you provide. It was useful.
Your example works while others I tried don’t. Thank you so much.
Thank you very much. I found this example quite clear, compared to some others loose on the interweb.
I just downloaded the examples and tried running everything as is and I got “undefined” for all 4 variables. I tried with CS3 and CS4, any ideas on why that happened, is there some kind of setting that I need to enable? Please help…
The flashvars will show undefined if you’re running from flash. They way to pass flashvars to the swf is in the embed code. So to actually test it you have to actually put it into an html file, good luck!
How do I load xml through flashvars?
http://blog.circlecube.com/2008/04/tutorial/xml-and-flash-actionscript-made-easy-parse-xml-to-object-tutorial/ – here’s one example I’ve already written about. There are plenty of ways to do it and the best way for you may depend on a few factors (for starters, as2 or as3?)
Nice clean tutorial and the examples work for me, but I wonder why as I can’t use the same approach in my class file.
Why doesn’t the AS3 compiler complain that these variables are undefined?
@Tristan – notice that this is for as2. I have another tut for as3 and flashvars… http://blog.circlecube.com/2009/12/tutorial/flashvars-and-as3/
Hello,
The tut works great if just testing as is, but when I add the AC_FL_RunContent() script, with
‘flashvars’, ‘var1=here&var2=are&var3=my&var4=flashvars’,
added in the AC code, it does not display. I’ve been searching for hours to come up with a solution that will work for FlashVars.
Thanks much,
Greg
Have you looked at SWFObject? It works great and gives you a variety of options for displaying your SWFs.
@Greg – yes I love swfobject, it’s the only tool I use anymore for embedding flash content online. CS5 will actually use swfobject rather than AC_Active whatever trash it uses currently.