The following article assumes you have a solid understanding of Flash AS3 and the Flash interface and skips all basic Flash setup instructions and explanations in effort to provide you with quick, concise information.
When using flashvars locally, I used to create a temporary variable that holds the information needed to run the application offline. That way I could test it locally, and comment it out when it was ready to be pushed live. Here’s my original approach:
-
// function to retrieve any flashvars set in the html document
-
function getFlashVar(_var:String):String {
-
var paramObj:Object = LoaderInfo(this.loaderInfo).parameters;
-
var flashvar:String = String(paramObj[_var]);
-
return flashvar;
-
}
-
// variables to store flashvars values grabbed from the html ›› uncomment for live site
-
// var myFlashVar:String = getFlashVar("testFlashVar");
-
// for testing (comment out for live site);
-
var myFlashVar:String = "testFlashVar";
The problem with this technique is that it’s extremely easy to forget to comment/uncomment the testing/live variables before handing it off to your technical lead or publishing it to your site. Forgetting to reset the values obviously leads to broken flash files and angry techies.
Let’s avoid all of that by taking an extra step and setting up a conditional statement that checks to see if a flashvar exists; if it doesn’t exist, use the default value. The new approach looks like this:
-
// function to retrieve any flashvars set in the html document
-
function getFlashVar(_var:String):String {
-
var paramObj:Object = LoaderInfo(this.loaderInfo).parameters;
-
var flashvar:String = String(paramObj[_var]);
-
return flashvar;
-
}
-
// set a variable to hold the value of our flashvar
-
var myFlashVar:String;
-
function checkForFlashVars():void {
-
if (myFlashVar == null) {
-
// the flashvar doesn’t exist ›› assign a default value so we can publish and view the file locally
-
myFlashVar = "testFlashVar";
-
trace("flashvar is not present…using default value: " + myFlashVar);
-
} else {
-
// flashvar exists ›› get the flashvar from the HTML
-
myFlashVar = getFlashVar("testFlashVar");
-
trace("flashvar is present: " + myFlashVar);
-
}
-
}
-
// call the function
-
checkForFlashVars();
Copy and paste the above into a blank AS3 document, then publish it and you’ll see it runs off of the default value (output in the trace). Using this approach when flashvars are correctly configured in the HTML saves time, confusion and sanity. If you’re wondering why I’ve encapsulated the conditionals into the checkForFlashVars() function, it’s mainly for personal preferences. This way, if I need something else to happen before the flashvars calls (or more likely, after the flashvar values have been pulled in), I can place checkForFlashVars() anywhere I need to ensure nothing in my application fires until the values (or lack of values) have been detected and stored.
If you need to handle the values of multiple flashvars, a similar approach can be taken using an object to load and store the parameters (flashvars) passed in from the HTML:
-
// set a variable to hold the value of our first flashvar
-
var myFlashVar:String = "testFlashVar";
-
// set a variable to hold the value of our second flashvar
-
var myFlashVar2:String = "false";
-
// set an object to hold all of the parameters (flashvars) passed in from the html
-
var paramObj:Object=LoaderInfo(this.root.loaderInfo).parameters;
-
function checkForFlashVars():void {
-
// check to see if myFlashVar has a new value passed in from the html; if it does, use this value instead
-
if (paramObj.xmlpath) {
-
myFlashVar = String(paramObj.myFlashVar);
-
trace("flashvar is present: " + myFlashVar);
-
}
-
if (paramObj.myFlashVar2) {
-
myFlashVar2 = String(paramObj.myFlashVar2);
-
trace("flashvar is present: " + myFlashVar2);
-
}
-
}
-
// call the function
-
checkForFlashVars();
If you copy and paste this code into a blank AS3 document, you’ll notice no trace codes in the Output Panel. That result means no flashvars are present, and the default values for testing are being used. If you need to grab additional flashvars, duplicate the variables/conditionals above. Based on your programming preferences, a great way to organize multiple conditionals is to use a switch() statement. Either one of the above flashvars techniques can be customized and extended as needed.
Lee Brimelow outlines a simple approach on his Flash website:
-
// set a var to hold the value of the flashvar
-
var myFlashVar:String;
-
-
// if the flashvar exists…
-
if(root.loaderInfo.parameters["isLive"] != null)
-
-
// set our temp value equal to the flashvar embedded in the HTML
-
myFlashVar = root.loaderInfo.parameters["isLive"];
-
-
// trace out the value
-
trace("myFlashVar: " + myFlashVar)
…where “isLive” is a flashvar set in the swfObject in the HTML.
As with all programming techniques, there are many approaches that can be applied so solve the similar problems. For me, these are the most comprehensive and most reliable methods I’ve found.