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.

No related posts.

Category: AS2 6 comments »

6 Responses to “FlashVars and AS2”

  1. janosch

    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.

  2. Barklund

    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.

  3. Neh

    really good example…

  4. Abe Pazos

    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

  5. Presy

    Hey.. I’m working on an html site with an swf navigation bar, the thing is everytime we click on one of the buttons of the navigation, the html page refresh and the button regain its initial state.
    Here is the site : http://www.madeforbeirut.com/
    I’ve been told to use “flashvars” but i don’t know how to do it coz i’m not developer.

    Plz if anyone can give me the code for AS2 and tell me how it’s done.. it would be soooo helpful !!
    I hope my explanation is clear.
    Thanks for ur help!!! and hope to get an answer asap.

    Presy
    [email removed]

  6. Barklund

    Hi Presy,

    FlashVars are extra parameters added in the HTML, that you can read from within flash. On your site above, you would add them by adding an extra line in the HTML after:

                        'salign', ''

    So it becomes:

                        'salign', '',
                        'flashvars', 'myvariable=myvalue'

    You can the myvariable and myvalue to whatever you like.


Leave a Reply



Back to top

     

Get Adobe Flash playerPlugin by wpburn.com wordpress themes