Using FlashVars is a very useful way to load dynamic data from the URL or HTML page containing Flash swf file. This approach is mainly used to pass on some id or resource path such as xml file which contains further data.
Passing resources paths using FlashVars really makes life easier for a developer switching back and forth between Flash testing environment and deploying it on web for testing where the actual dynamic data is available.
Although it’s not that easier to pass FlashVars in AS3 as much it used to be in AS2. In the good old AS2 days, you just had to define the variable and use it anywhere you wanted. In AS3, there are number of extra steps but first add the variables to be passed on the html page.
I use the SWFObject to embed the swf on a html page. You can easily pass variables to the Flash Player this way:
//standard way of passing variables from SWFObject so.addVariable("flashVar", "value"); //I usually pass base url of the staging or real server like this from here so.addVariable("baseURL", "http://demo.your-website.com/");
Now inside Flash, in the following code snippet, baseURL variable of the staging url is being processed
var paramObj:Object = LoaderInfo(this.root.loaderInfo).parameters; var baseURL:String = String(paramObj["baseURL"]); //Now check it if it's undefined, you can reset it to blank if(baseURL=="undefined") baseURL=''; /* Since baseURL is blank now, you can easily keep working offline and load the flash resources locally. Otherwise it will concatenate to the local path and load from the staging URL. */ var xmlPath:String = baseURL+"flash-data/resources.xml"; var thumbsPath:String = baseURL+"flash-data/thumbs/"; var videosPath:String = baseURL+"flash-data/videos/"; /* You can easily further concatenate thumbs and videos path by parsing xml file, if they are defined there but you get the main idea here. */
This technique allows keeping the same swf file functional both offline in Flash test environment and inside html page with SWFObject speeding up the development process.
Hope that helps.
Cheers!