AS2 → AS3: Retrieving FlashVars
I was recently working on the Image Slider that I posted here a couple of days ago and I realized I didn't know how to access FlashVars in AS3. I quickly did a Google search and came up with the solution rather easily.
Below is the code for the brand new SWFObject 2.0 way to embed an SWF:
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
-
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
-
-
<title>FlashVars Test</title>
-
-
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
-
-
<script type="text/javascript" src="scripts/swfobject.js"></script>
-
<script type="text/javascript">
-
swfobject.embedSWF("swf/AS3.swf", "flashcontent", "550", "400", "9.0.0", false, {param1: "parameter one value", param2: "parameter two value"}, {menu: "false"}, {id: "flashID", name: "flashvars-test"});
-
</script>
-
-
<link REL="stylesheet" TYPE="text/css" HREF="scripts/styles.css" TITLE="styles">
-
-
</head>
-
-
-
<div id="flashcontent">
-
If you have Javascript disabled, please enable it now.
-
</div>
-
-
</body>
-
-
</html>
As you can see, the embedSWF method gets passed in a bunch of parameters. Among those, you can pass an object that is your FlashVars, in this case "param1" and "param2". These are the values we want to grab out of the FlashVars and get inside of our FLA. The code to do this in AS2 used _level0 (or _root), which is no longer available in AS3.
-
info_txt.text = _level0.param1;
-
info_txt.text += "\n" + _level0.param2;
In AS3 you use the loaderInfo property of a DisplayObject, in this case our main stage. The loaderInfo property is part of the LoaderInfo class which in turn has a parameters property which allows you to access the FlashVars we discussed earlier.
-
info_txt.text = this.loaderInfo.parameters.param1;
-
info_txt.text += "\n" + this.loaderInfo.parameters.param2;
This is a pretty simple approach and very easy to do. It isn't as short as the AS2 version (which seems to be the case with many of these conversion basics), but it makes a lot more sense than using levels and such.
If you found this post useful, please consider leaving a comment, subscribing to the feed, or making a small donation.











[...] A brief article on this can be found at: http://evolve.reintroducing.com [...]