Access FlashVars parameters with AS3

Many people use FlashVars as a way to send parameters to the flash movie playing in the browser. This way the parameters are imported as root level variables.

Using ActionScript 1 and 2 these variables can be accessed easily from the root level, like this: _root.parameterName. So, if you would send two parameters and their values (xmlFile=setup.xml&imageFile=picture.jpg) to the flash movie, you would access their values like this:

trace(“the xml file is: “+_root.xmlFile);
trace(“the image file is: “+_root.imageFile);

This changes when it comes to ActionScript 3. You cannot access _root.xmlFile or _root.imageFile, simply because these variables do not exist on the root level. They haven’t been declared so the compiler throws an error. The new way to access the FlashVars parameters using ActionScript 3 is to use the LoaderInfo class. Every DisplayObject contains a property called loaderInfo which is an instance of the LoaderInfo class. An object of this class contains a property called parameters. This is an object that enumerates the list of FlashVars parameters and their values. You would access these parameters like this:

var paramList:Object = this.root.loaderInfo.parameters;
trace(“the xml file is: “+paramList[“xmlFile”]); // you could also use paramList.xmlFile
trace(“the image file is: “+paramList[“imageFile”]); // and paramList.imageFile

14 thoughts on “Access FlashVars parameters with AS3

  1. Hi,

    What if the class I’m trying to load from isn’t a DisplayObject? In AS2, _root[filename] worked, but now that I’m trying to load from a non-DisplayObject class, I can’t use this.root. Suggestions?

  2. To Michael G.

    It seems that the only way to access the root object is through a Display Object(at least this is what the documentation says). Even if you are using non-DisplayObject classes, inside them you need to reference a DisplayObject that’s found on the Stage. Nothing else comes into my mind right now. If anyone else has a better idea, you’re welcome to post a comment.

  3. Excellent! I have been looking for some information on how to use FlashVars with AS3, and your tutorial was the first I found from Google that explains it in an easy to digest way. Thanks!

  4. Just wanted to clear something up with regards to the root issue…

    I will be honest, never had to reference the root in any AS3 code I have written, and in terms of the flashVars / parameters, this.loaderInfo.parameters or loaderInfo.parameters is all that’s required here.

    e.g

    var _params:Object;

    if ( this.loaderInfo.parameters.length != null ) {
    _params = this.loaderInfo.parameters;
    } else {
    // run alt code to init
    }

    Place the above code inside your document class constructor or frame 1 scripting window.

    If you want to trace out your results use this;

    for ( var sItem:String in _params ) {
    trace( “Name : ” + sItem + “, Value : ” + _params[ sItem ] );
    }

    No root reference needed! Not a display object in sight!

    Hope that helps.

  5. Thanks, R_A_H_

    I also usually use notation without the root property, but for those that are just getting into AS3 and are coming from AS2, I thought it would be a good example, so they see and understand where the FlashVars parameters would be found. I guess I should have explained it a little better: for display objects, root is he topmost object in the display list – so they can always be sure that they have a reference to that topmost object. The loaderInfo object is defined only for the root display object – that’s what I wanted to emphasize.

    Thank you for your comment. Much appreciated.

  6. Ah I see your point, understood, I can appreciate why you would do that, no disrespect intended. ;-)

    Thanks.

  7. Oh, don’t worry R_A_H_, none taken :)

    I’m happy that you left the comment. Your information is quite useful to others that might read this article, especially to newbies. So thanks again.

  8. Pingback: Web Design and Development usefull links | Victorelu

  9. Pingback: calling a php variable in as3 - SitePoint Forums

Leave a Reply

Your email address will not be published. Required fields are marked *

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>