I use SWFObject to add Flash elements to any given page that I work on containing Flash elements. SWFObject is one of the most elegant ways of handling Flash content. It uses a single div with basic CSS and a slim JavaScript file to:
- determine if the user has Flash and if so, which version they are running
- upgrade the user’s version of Flash with ExpressInstall (optional)
- quickly pass in external variables (from your page) to your Flash file.
- easily configure requirements, and if the user has Flash (or was just upgraded) the Flash content is displayed; otherwise, alternate text or images are used.
To pass variables into a Flash file using SWFObject you add the following line of code to your SWFObject javascript block:
1 |
soFlash.addVariable("msg", "this data came from an external source"); |
Now that a variable has been passed in via SWFObject, we can access it in Flash using ActionScript 3. If you’ve just started your first ActionScript 3 project (or you’re porting over AS2 code to AS3) and you’re scratching your head as to why your FlashVars are now undefined
in the _root
, it’s because they don’t live there anymore (in fact _root
no longer exists! It is now just root
without a leading underscore _
).
The FlashVars are now available in the new LoaderInfo.parameters
object. What you can do is create a single object that will store all external variables as properties of your object as demonstrated by the following code:
1 2 3 4 5 6 7 8 9 10 |
var allFlashVars:Object = LoaderInfo(this.root.loaderInfo).parameters; //******************************************************************************** // To access your variable names all you do now is refer to the // variable name as a property of the object created above // For example, to access an external variable named "msg" you would write: // // allFlashVars.msg; // //******************************************************************************** |
Here’s the full code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
// All that is on stage is a TextField with an instance name of "output" // Sure, we could have made this TextField using code, but the code might clutter // what we're really here study. // All of our external variables will be stored in this object // Then we're going to loop over this object using a for...in loop var allFlashVars:Object = LoaderInfo(this.root.loaderInfo).parameters; //******************************************************************************** // To access your variable names all you do now is refer to the // variable name as a property of the object created above // For example, to access an external variable named "firstName" you would write: // // allFlashVars.firstName; // //******************************************************************************** // // For the purposes of this demonstration, we will output all of the external // variable names and values //Create a placeholder to keep the current variable name and variable value var theVariableName:String; var theVariableValue:String; for (theVariableName in allFlashVars) { theVariableValue = String(allFlashVars[theVariableName]); output.appendText("\t" + theVariableName + ":\t\t" + theVariableValue + "\n"); } //That's it! Happy coding! //Cheers, //Jonathan |
Download the sample files (.zip)
Post any questions in the comments!
Man, thanks a lot for the tip! Now all variables in my movie are accessible! 🙂
Now, time to discover how to make LightWindow call from inside the movie… wish me luck! 😛
Diogo
Glad you found the info useful! I’m not familiar with LightWindow but best of luck to you on the implementation! If you have any questions, just post them in the comments.
Cheers,
Jonathan
YAY!!! thank you : you really helped me!
Very nice! This has helped me immensely!
thanks! just what I needed.
This was really a huge hand — thanks a lot! After searching for a looonnng time, you seem to have the only post on the Web that hits on this 🙂
Hi! have used something similar in an e-card movie, but can’t get it to work. Would love your feedback, can I send code?
I looked for something that did this for ages. Thank you so much.
Can you tell me how to access the individual variables that get loaded in? It may be a very basic thing (I appologise if it is) but I can’t seem to figure it out.
Thanks again
Ignore my last comment, I’ve found out how to do it… doh
Thanks again though.
ok so if i am tring to pass a var like this:
DL_AUTH_USERNAME on a sendVars object
It is sent like this DL%5FAUTH%5FUSERNAME
example:
var dataOut:LoadVars = new LoadVars();
function checkUser():Void {
dataOut.DL_AUTH_USERNAME = loginForm.userName_txt.text;
trace(dataOut); // is DL%5FAUTH%5FUSERNAME
}
How do you get the _ to pass correctly? Thanks for any insight
Thank you! After clicking on multiple sites from a google search, this one finally made sense.
I am having a little trouble with the communication… Maybe you could look at some code and find my misstep. Thanks for the info, very helpful…
HTML:
var so = new SWFObject(“Hilti360.swf”, “Hilti360”, “540”, “360”, “8”, “#ffffff”);
so.addVariable(“myUrl”, “index.html”);
so.write(“flashcontent”);
AS3:
var xmlFile:String;
xmlFile = root.loaderInfo.parameters.myUrl
trace(myUrl);
trace(xmlFile);
var myUrl:String;
//trace(currenturl);
//var xmlFile:String;
if (myUrl == “index-FR.html”) {
xmlFile = “content/FR.xml”;
trace(“FR”);
} else if (myUrl == “index.html”) {
xmlFile = “content/EN.xml”;
trace(“EN”);
} else {
xmlFile = “content/PT.xml”;
trace(“PT”);
}
var xmlLoader:URLLoader = new URLLoader();
var xmlData:XML = new XML();
xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
xmlLoader.load(new URLRequest(xmlFile));
function LoadXML(e:Event):void {
trace(“LoadXML”);
xmlData = new XML(e.target.data);
ParseContent(xmlData);
}
@Josh: Your AS creates the string “xmlFile” which contains the value of the external variable “myUrl”. Your if statement then compares the value of a local variable named “myUrl”. As such, I’m assuming you’re always seeing “PT” in the output panel. So, I would think your code should look a little more like this (hope it helps!):
//Create a reference to the xml file to be loaded.
var xmlFile:String;
//Store a reference of the external URL passed in.
var myUrl:String;
myUrl = root.loaderInfo.parameters.myUrl
switch(myUrl){
case “index-FR.html”:
xmlFile = “content/FR.xml”;
break;
case “index.html”:
xmlFile = “content/EN.xml”;
break;
default:
xmlFile = “content/PT.xml”;
break;
}
Thanks a lot!, yet with this code:
var xmlFile:String;
trace(xmlFile);
var myUrl:String;
trace(myUrl);
myUrl = root.loaderInfo.parameters.myUrl;
trace(root.loaderInfo.parameters.myUrl);
switch (myUrl) {
case “index-FR.html” :
xmlFile = “content/FR.xml”;
trace(“FR”);
break;
case “index-PT.html” :
xmlFile = “content/PT.xml”;
trace(“PT”);
break;
default :
xmlFile = “content/EN.xml”;
trace(“EN”);
break;
}
I receive this output:
null
undefined
null
EN
…and only the default case is shown. Did I enter something wrong?
Thanks Again!
Sorry,
this output:
null
null
undefined
EN
The first two traces display “null” because you are tracing the variables before any value is set.
You are getting “undefined” in your third trace because you are testing within the Flash IDE. “myUrl” is passed in via JavaScript, which means you need to view this page in a browser so that you can pass the variable in via SWFObject.
If you want to test within the Flash IDE, just check if myUrl is undefined and if so, set it to whatever you want.
—————————————
//Try something like this:
var xmlFile:String;
var myUrl:String;
myUrl = root.loaderInfo.parameters.myUrl;
//If you want to test within Flash, set a default value of
//any external variables
if(myUrl == undefined || myUrl == null){ myUrl = “index.html”}
switch (myUrl) {
case
That makes sense. I am still only able to show the default case, regardless of which html page a preview. Do I need to define something else in the html?
Thanks!
Hey,
how can i readout the parameters of a swf-file within an other Swf-FIle?
like:
SwfA loads SwfB and i wanna know, wich Vars are passed to SwfB.
greets from Germany 😉
Thanks for your tutorial. I dont understand how to make flash understand spaces in text.
“Thisiswhatigot” and “This is what i need”
my1.text = this.loaderInfo.parameters.my1;
What should i add?
Thanks
@Georgiy: Do you have a sample of your code/swf posted somewhere?
Sorry! I found the solution! I didn’t emd font (space) for texteff=))
But another one question, may be you’ll help=) if not! it’s ok! thanks a lot for you tutorial.
Question: i have dynamic text area named my_txt.
what should i write in actionscript to change fontcolore like i get content for text variable
(my_txt.text.color = this.loaderInfo.parameters.my_txt_color;) – dont’t work
or for movieclip tint color. something like this
(i have movieclip named bg and i want to get tint color dynamicly from html)
something like this:
bg.tint = this.loaderInfo.parameters.bg_tint;
(bg.tint = #ff0000) – for a example!
Thanks a lot in any way!
Georgiy:
Good AS3 questions and very common for new developers. Here is how I would approach the tinting of a movieclip and textfield color….
Hope that helps and happy coding!
Thanx Guru….
Thanx a lot…..:D
Very good example. Can I make a suggestion, you use:
rather than keep your own copy?
url did not appear in previous post, try again
http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js“
THANK YOU!
all other examples failed – this finally works!
🙂
Nice work. Many thanks for your efforts.
This still helps today, Cheers! Most other examples look at the js side way to much and assume you can work the as3 stuff out.
Hi this was very helpful! I do have a question:
If I’m setting variables in the js using swfObject as you did in your sample, how can I make it so that flash reload those variables on a 5 second interval?
I set up a timer in flash, which works, but only for the initial pull of the variables. after the timer launches the function that uses the variables loaded, it doesn’t update.
This is the same issue I had using flashVars, my understanding was that swfObject removes the limitation of only loading the vars on document load. is this not the case?
Thanks in advance!
God bless you man! You saved my brain!
Huge help! Thank you very much. Now I can finally move forward on my project.
I get an error when I’m trying to implement it in my own swf.
Error 1120: Access of undefined property output
output.appendText(“t” + theVariableName + “:tt” + theVariableValue + “n”);
I’m a noob 😎
Hello,
I just tried to use your script, but as soon as i add a TLF text into the scene, not linked at all to your script, just added, it breaks your script. Variables are not red anymore, they don’t appear in the TextField.
I’ve spent 2 days trying to find the problem.