Posted on Thursday 2 November 2006
A very common irritating factor when using Flash is FlashVars. When you test your movie inside the IDE you have to specify default parameters. So that these parameters don't override the ones in the final movie, people have resorted to placing the variable initialization inside of a if(CustomActions) block. But then when playing the swf by itself outside of the IDE the variables aren't picked up. Another issue is the fact that all the variables arrive as strings, and then you have to type them correctly.
So here is a sane approach to FlashVars:
import cinqetdemi.JSON;
var myFlashVars = _level0.flashVarsDefault;
if(typeof(_level0.flashVars) == "string")
{
try
{
myFlashVars = JSON.parse(_level0.flashVars);
}
catch(error:Error)
{
myFlashVars = null;
trace(error.toString());
}
}
To make this works, you have to set up a flashVarsDefault variable on the root timeline. This should be an object, such as:
var flashVarsDefault = {edition:3, numPreloaded:2, config:"/config"};
When you want to use FlashVars, just use one key called flashVars:
And there you are: typed FlashVars that will work in the IDE, as a standalone swf and embedded in HTML. You will need the JSON parser available here.


