I do a lot of work with on-line apps that are customized at run time through Flash Vars. So I’ve devised a lot of different ways to fake them while testing in the IDE. At the end of the day, a Flash Var is simply a variable defined on _root.
Thus, the simplest way to simulate them is to create a variable on the main time line:
user_id = 1;
The problem is that I often have a bunch of these and when it comes time to deliver the final swf, these variables will overwrite the actual Flash Vars coming in. So they have to be commented out, or otherwise disabled. One thing I was doing for a while is this:
if(user_id == undefined) { user_id = 1; }
This has the added benefit of giving you a default value if no actual Flash Var is provided. Still, sometimes you don’t want a default value. And I hated going back and forth from my editor to the IDE timeline actions panel.
My second attempt was to just move the Flash Var declarations into the class itself, in an init function, or a setFlashVars function:
// in class: _root.user_id = 1;
It took some gritting of teeth to get over the fact that I was writing “_root” within a class, but the use of Flash Vars is probably the one case where I consider it acceptable.
I still didn’t like mixing this stuff into my application class though. It’s not really part of what the class is responsible for. So I moved it into its own FlashVars class, which looks something like this:
class com.whatever.FlashVars { public static function simulate() { makeTest01(); // makeTest02(); // makeTest03(); } private static function makeTest01(Void):Void { _root.user_id = 8; _root.section_id = 31; } private static function makeTest02(Void):Void { _root.user_id = 9; _root.section_id = 11; } private static function makeTest03(Void):Void { _root.user_id = 85; _root.section_id = 341; } }
And in my class I just say:
com.whatever.FlashVars.simulate();
This allows me to create as many different configurations as I want, and easily test any one of them. And in the final run, I just comment that one line out of my class and publish.
Actually to bullet proof this you need to correct one thing:
_level0 is the true destination for these vars, not _root. If for some reason levels are being used the above will not work.
Good point. My applications have not loaded things into levels, so I never ran into that. Thanks.
Make it even better:
if( CustomActions )
{
// set _level0 variables
}
This way, no need to comment anything out (as CustomActions is only available within the test player in the IDE).
Nice. Keep ’em coming. I guess by commenting it out, the FlashVars class doesn’t get compiled into the swf at all. But it’s probably minimal anyway.
But you should consider this:
Applications should always have a preloader. This is best done in a separate file, which loads the application itself (using _lockroot in a given loading MC) so now using _root would point to the MC and not _level0 which is where the flashVars are in. So u should probably rethink your class.
I disagree with your first two statements. Some applications should have preloaders, but not all by any means. And when I do use a preloader, I use a multiframe movie with assets on one frame, classes exported on another, and a preload loop in the beginning. Not to say that there’s anything wrong with the external loading in method, but “best” is subjective. But yeah, if you are using a preloader, and doing it that way, you’d have to do it differently. Then again, you’d have to access your flashvars differently. As long as you are accessing them the same as you are faking them, you should be fine.
Great idea. It could be better like that :
class FlashVars {
public static function simulate (vars:Array, values:Array) :Void {
var l:Number = vars.length;
for (var i=0; i<l; i++)
if (_root[ (vars[i]) ]==undefined) {
_root[ (vars[i]) ] = values[i];
}
}
}
Hmmm… Could be useful. For me, I like having the different preset configurations. Anyway, just some free-floating ideas I’m putting out there. Use or revise or ignore as much as you want.
Your post is right on time for me.
Implementing the class as we speak.
FWIW, I typically just test for the player type and set variables accordingly…
if (System.capabilities.playerType == “External”)
{
// set fake flashvars here
}