SWFObject.addVariable() in AS3

SWFObject.addVariable() in AS3

SWFObject is the standard for embedding Flash files into HTML. If you haven’t used it run on over to Geoff Stearns site and download yourself a heapin’ helpin’ of some SWFObject. If you are feeling especially adventuresome and want to test out the latest features in the new SwfObject 2.0 then head over to the SwfFix development blog. SWFObject eliminates the need to click on the Flash piece on a website before being able to interact with it, it has plug-in detection, it can be used with popular blogging software on sites such as this, and there are plugins for Dreamweaver and as a publish setting right in Flash. You should be using this for every website you build.  As of the 15th of February (I wonder what they were doing on Valentines Day) you can even download an SWFObject Adobe Air app for generating the proper code to embed your SwfObject into your website.  It employs the new SWFObject 2.0.  Or for quick and easy access just use the web version to generate your swfObject code

Now that you are totally sold and will never build anything Flash without it, you may want to inject variables (FlashVars) into your swf.  The methods are different for AS2 and AS3.  It is quite easy, yet different in each of these versions of ActionScript.  You simply throw the following code into the HTML and then …

<div id=”flashcontent”>
Swf Audio is not loaded Please check your path. this should be an audio control built in flash.</div>

<script type=”text/javascript”>

var so = new SWFObject(“SoundControlTest2.swf”, “audioControl”, “240”, “24”, “8”, “#EFECDD”);
so.addVariable(“audioURL”, “AllThatIWant.mp3”);
so.addVariable(“audioURL2”, “Comfort.mp3”);
so.addVariable(“audioURL3”, “Jolene.mp3”);
so.addVariable(“audioURL4”, “Amazing.mp3”);
so.write(“flashcontent”);
</script>

…then in your Document class (or on the MainTimeline if you are not using a Document Class) you will need to reference the parameters property of the loaderInfo property like this:

myTextBox.text = this.loaderInfo.parameters.audioURL;

In AS2 you only can access the variable directly on the mainTimeline using something like this:

myTextBox.text = audioURL;

Hope this helps you (and your users) have a better Flash experience on the web.

As always Happy Flashing.

Curtis J. Morley