in Flash, Programming

Using FlashVars with SWFObject in ActionScript3

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:

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:

 

Here’s the full code.

Download the sample files (.zip)

Post any questions in the comments!

Write a Comment

Comment

33 Comments

  1. 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

  2. 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

  3. 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?

  4. 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

  5. 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

  6. 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);
    }

  7. @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;
    }

  8. 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!

  9. 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

  10. 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!

  11. 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 😉

  12. 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

  13. 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!

  14. 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!

  15. 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.

  16. 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!

  17. 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 😎

  18. 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.

Webmentions

  • Audio-Player von aussen steuern? - Flashforum January 20, 2011

    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.