Going from AS3 (Actionscript 3) to AS2 is a bit of a struggle at times and at “work”:https://www.llnw.com I’m mainly in AS2 for a few projects I’m working on. So, one of the things I’ve been doing is creating every app using events so I have a base EventDispatcherBase class that mimics the AS3 EventDispatcher. All classes, that need to, extend EventDispatchBase and, to follow how things go with AS3, your event names are constants of either the event or your class/mxml file.
For example, you would dispatch an event like this in AS3:
bq. dispatchEvent(new Event(Event.COMPLETE));
In AS2, you dispatch like this:
bq. dispatch({type: “MyType”, target: this});
So, my EventDispatchBase has a dispatchEvent function which does the above by takes an IEvent (interface) as an argument. My event dispatching in AS2 looks like this:
bq. dispatchEvent(new Event(Event.COMPLETE, this)); //where “this” is the target in most cases
I added the “this” argument for specific cases where I wanted to specify a child of my present class as the target.
Now…how do I keep all of my event names in check AND insure new developers working with the code do not jack up my event calls? Constants. Well…wait…AS2 doesn’t have constants so how do I use them? I’m glad you asked.
bq. public static function get PLAY_VIDEO():String{ return “PLAY_VIDEO”; }
A getter function is nothing more than a read-only property. Using a getter with no setter ensures your event name cannot be changed. One thing to not is I’m returning the string direct vs storing it in a variable.
bq. private var _playVideoEvent:String = “PLAY_VIDEO”;
public static function get PLAY_VIDEO():String{ return _playVideoEvent; }
The reason is due to AS2’s loose nature of allowing you to access private vars in a class by using code like this:
bq. trace(myclass[“privatevarname”]);
Anyways…this is my little rant for the day. I have all sorts of lil’ random information I want to blog about daily but I never take the time to do it. I’ll try to do it more often though.