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

13 thoughts on “SWFObject.addVariable() in AS3

  1. Hi Curtis,

    Thank you very much for sharing it.

    I am PHP programmer and now I am learning flash actionscript. This helped me a lot.

    Thanks again,
    -PM.

  2. Pingback: xml per swj.Obj.addVariable in Flash auslesen! - Flashforum

  3. Thank you for posting this info. I scoured the internet for 6 hours looking for this one little piece of information. Every other site was like. It’s so easy you just add this to your HTML and that’s it.

    There seemed to be a mass amount of forgetting to add the key to how it actually works.

    Now my bosses temperature will drop and I can feel like I’m doing a good job again.

    THANKS SO MUCH FOR BEING THOROUGH.

  4. Oliver,

    Thanks for the comment. It seems that you have a valid comment. You could have said it a little nicer, but I added what was missing.
    Thanks for visiting the site.

    Curtis J. Morley

  5. Aubby,

    Great question. The difference between addParam and addVariable is that addParam allows you to change the swf within the HTML and addVariables allows you to pass in variables through FlashVars.

    Examples of changing the parameters are:
    so.addParam(“quality”, “low”); – Low Quality rendering int he swf
    so.addParam(“wmode”, “transparent”); Allows the background to be transparent.
    etc…

    I hope this helps.

    Thanks,
    Curtis J. Morley

  6. I wouldn’t call this a “crappy” explanation.

    This gets you 90% of the way there, a simple google on the line “this.loaderInfo.parameters” would have gotten you the rest of the way. Stop whining and do your research.

    Thank you very much Curtis.

    I do have one question: Are SWFObject.addVariable and SWFObject.addParam synonymous?

  7. this is a pretty crappy explaination. what am I supposed to do with “this.loaderInfo.parameters.audioURL;”?
    can I use it as:
    myTextBox.text = this.loaderInfo.parameters.audioURL;
    A better working example would have been more useful.

Comments are closed.