Warning: include_once(/home/quipnet/quip.net/blog/wp-content/plugins/wordpress-support/wordpress-support.php): failed to open stream: Permission denied in /home/quipnet/quip.net/blog/wp-settings.php on line 212

Warning: include_once(): Failed opening '/home/quipnet/quip.net/blog/wp-content/plugins/wordpress-support/wordpress-support.php' for inclusion (include_path='.:/usr/local/lib/php:/usr/local/php5/lib/pear') in /home/quipnet/quip.net/blog/wp-settings.php on line 212
David Stiller’s blog » Blog Archive » How to Retrieve FlashVars Data in ActionScript 3.0

How to Retrieve FlashVars Data in ActionScript 3.0

ActionScript 3.0 Flash

Last year around this time, I described how to pass variables into a SWF by way of something called FlashVars.  This was discussed in “How to Tell a SWF What File(s) to Load — From the Outside” which works for pretty much any sort of simple data you may want to pass in, as long as you’re talking about relatively small groups of name/value pairs.  Complex data is more likely to be suited to XML.  In that earlier blog entry, the purpose was to re-use a single SWF numerous times to load a variety of MP3 files, by specifying which audio in the HTML.  Pretty flexible solution!  You could use the same technique for loading JPGs or FLVs, to populate dynamic text fields, or whatever you like.  The article has been fairly popular and continues to be useful — as long as your SWF is published for ActionScript 2.0 (or even 1.0 if you drop the post colon suffixes — :String, :Number, and the like).  In ActionScript 3.0, things are different. 

The updated code

There are two halves to getting this sort variable into Flash:  the HTML side and the ActionScript side.  Fortunately, the HTML side hasn’t changed at all.  Even with an ActionScript 3.0 SWF, you can read the existing “From the Outside” article and continue to follow the instructions as stated, even for the SWFObject (JavaScript embedded SWF) section at the end.  The difference comes within the first few paragraphs on ActionScript 2.0.  And even so, the code as shown in that article …

But clearly, this approach is hard coded.  In order to open up the possibilities, replace the path reference with a variable — let’s just call it audio.

var s:Sound = new Sound();
iconButton.onRelease = function():Void {
 s.loadAudio(audio, true);

… comes fairly close to the updated syntax.

In ActionScript 3.0, incoming FlashVars variables are no longer available as a loose collection in the main timeline.  In the above AS2 code, the audio variable is passed in to the SWF from the HTML document and is therefore not declared in the movie.  In AS3, such variables have been moved to the parameters property of a LoaderInfo instance associated with the main timeline.  The LoaderInfo class “provides information about a loaded SWF file or a loaded image file (JPEG, GIF, or PNG).  LoaderInfo objects are available for any display object [this means instances of the DisplayObject class, which includes movie clips].  The information provided includes load progress, the URLs of the loader and loaded content, the number of bytes total for the media, and the nominal height and width of the media.” source

To get to the LoaderInfo instance associated with the main timeline, you may refer to the DisplayObject.loaderInfo property of the main timeline.  Remember, the main timeline is a descendent of the DisplayObject class; it can be treated as a movie clip (an instance of the MovieClip class) or a sprite (an instance of the new Sprite class) — but we don’t need to worry about that here.  The punch line is simply that instances of DisplayObject have a loaderInfo property that points to their own private instance of LoaderInfo, and LoaderInfo instances have a parameters property.  To update the above AS2 code, then, swap out the solitary audio variable with the expression root.loaderInfo.parameters.audio.  The name of the variable is the same, assuming FlashVars specifies it as “audio.”  Here it is in context:

var s:Sound = new Sound();
iconButton.onRelease = function():Void {
 s.loadAudio(root.loaderInfo.parameters.audio, true);

Now, even with that change, the above code would fail in ActionScript 3.0.  Why?  Because AS3’s event handler model is completely overhauled.  Button instances, like the above iconButton, no longer use onRelease as shown, plus the Sound class behaves differently.  Here’s the same brief code in AS3:

var s:Sound = new Sound();
 function(evt:MouseEvent):void {
  s.load(new URLRequest(root.loaderInfo.parameters.audio));

But that isn’t the important part, because ultimately, what you do with passed-in variables is up to you.  The key is how to access them, and the solution is root.loaderInfo.parameters, followed by a dot and the name of whatever variable you’re after.  This variable will have been passed into Flash by way of the FlashVars attribute of your HTML’s <object> and/or <embed> tag(s).

Leave a Reply

Warning: require_once(/home/quipnet/quip.net/blog/wp-content/themes/default/footer.php): failed to open stream: Permission denied in /home/quipnet/quip.net/blog/wp-includes/theme.php on line 1115

Fatal error: require_once(): Failed opening required '/home/quipnet/quip.net/blog/wp-content/themes/default/footer.php' (include_path='.:/usr/local/lib/php:/usr/local/php5/lib/pear') in /home/quipnet/quip.net/blog/wp-includes/theme.php on line 1115