FlashVars and AS2
When using FlashVars variables are pushed directly from the HTML-embedding of the Flash instance into the main timeline – _level0 that is.
To avoid having _level0.some_flashvar spread around the code without knowing, whether they exist, where they are used and more, I normally use a dynamic singleton FlashVars-class for first parsing these in the main timeline, and subsequently using this singleton class when loading vars.
The class is defined as:
- dynamic class org.barklund.utils.FlashVars {
- /* vars */
- private static var instance:FlashVars;
- private var vars:Object;
- /* can be overriden or cleared by inheritance */
- private var prefix:String = "fv_";
- /* constructor */
- private function FlashVars() {
- // init hashmap
- vars = new Object();
- // set __resolve to use getVar-function for dynamic references
- __resolve = getVar;
- }
- /* singleton */
- public static function getInstance():FlashVars {
- return instance ? instance : instance = new FlashVars();
- }
- /* parse through movieclip */
- public function parse(p:MovieClip):Void {
- for (var i:String in p) {
- if (prefix) {
- if (i.indexOf(prefix) !== 0) {
- continue;
- }
- setVar(i.substring(prefix.length), p[i]);
- } else {
- setVar(i, p[i]);
- }
- }
- }
- /* get and set */
- private function setVar(n:String, v:String):Void {
- vars[n] = v;
- }
- public function getVar(n:String):String {
- return vars[n] !== undefined ? String(vars[n]) : null;
- }
- }
And it can be used as for instance on the main timeline:
- import org.barklund.utils.FlashVars;
- FlashVars.getInstance().parse(this);
And variables can be read like:
- import org.barklund.utils.FlashVars;
- class org.barklund.someproject.SomeClass {
- ...
- var fv:FlashVars = FlashVars.getInstance();
- var id:Number = parseInt(fv.id);
- var name:String = String(fv.name);
- }
It can also be extended with a non-dynamic class for strong typing (though it requires extending singleton-ness, which is quite strange-looking):
- import org.barklund.utils.FlashVars;
- class org.barklund.someproject.SomeSettingsClass extends FlashVars {
- /* override singleton */
- public static function getInstance():SomeSettingsClass {
- return SomeSettingsClass(instance ? instance : instance = new SomeSettingsClass());
- }
- /* public accessors */
- public function get id():Number {
- return Number(getVar("id"));
- }
- public function get name():Number {
- return Number(getVar("name"));
- }
- }
This class is not dynamic in order to make the user of this only use these two variables. Other variables from the FlashVars-object can be read via getVar though (inherited publicly), so this could also be redeclared as private.

























janosch said
at June 6 2006 @ 13:03
Thx, nice idea!
You could overwrite the instance-variable too:
private static var instance:SomeSettingsClass;
public static function getInstance():SomeSettingsClass {
return instance ? instance : instance = new SomeSettingsClass();
}
So you can omit the SomeSettingsClass-cast.
Barklund said
at June 6 2006 @ 13:10
Yes, that would be possible, but I’m not sure if it makes it better. At least that would separate the two singletons completely thus creating nicer code, but I’m not sure whether a separation is desirable.
Neh said
at June 7 2006 @ 05:30
really good example…
Abe Pazos said
at January 3 2007 @ 13:06
Hi! Thanks for sharing!
What would be your approach to simulate FlashVars using this class when outside HTML? (inside FlashDevelop for example)
Thank you + happy 007
aBe