Passing Variables from HTML to Flash (flashVars)

First of all, why the heck would you want to do this?  A couple of reasons:

  1. It gives the ability for someone else to modify the Flash content without knowing Flash!
  2. It keeps you from making many different swfs for projects that are highly similar.  i.e. If you need to create many different video players because they’re all going to play different clips.  Well, just make the content path to the video a variable in HTML!

Here’s how you do it:

In HTML, add this parameter to your embedded swf object:
value=”videoName=testVideo1″ name=”FlashVars”

If you wanted to add more parameters you’d write it this way:
value=”videoName=testVideo1&theLogo=kentwood” name=”FlashVars”

 

In Flash, create a Flash File - ActionScript 3.

In the first frame place two dynamic text fields with the name videoNameText and theLogoText on the stage.

Add this code in your Actions panel. 

this.loaderInfo.addEventListener(Event.COMPLETE, loaderComplete);

function loaderComplete(myEvent:Event) {
   var flashVars=this.loaderInfo.parameters;
   videoNameText.text = "videoName from HTML: " + flashVars.videoName;
   theLogoText.text = "theLogo from HTML: "+flashVars.theLogo;
}

Publish your swf and HTML. 

Place the HTML parameters in the HTML code.

Preview in Browser. You should see the following in the text fields

videoName from HTML: testVideo1
theLogo from HTML: kentwood

With what you’ve learned above you’re able to control content in Flash from HTML!  Very handy.

error

Enjoy this blog? Please spread the word :)