Using EventDispatcher in AS2, the AS3 way
After a long run of AS3 goodness, I was outsourced to do some dinosaur development at these-days… An AS2 project, which unfortunately got cancelled after just 4 days :(.
It wasn’t a total waste of time though… Coming back from ActionScript3 to 2 brought along a number of “better” programming practices
The best example that popped up is event handling… Sure, event bubbling etc. is out of the question, but some basics can be imitated with relatively nice results.
In AS3, you extend mx.events.EventDispatcher (or implement flash.events.IEventDispatcher) to (surprise surprise) dispatch events… The “old way” of achieving this in AS2 was to “shove in” some clunky EventDispatching code… So, simply shove that clunkiness into a class:
[as]
import mx.events.EventDispatcher;
class AS2EventDispatcher{
public function AS2EventDispatcher(){
EventDispatcher.initialize(this);
}
public function dispatchEvent(){}
public function addEventListener(){}
public function removeEventListener(){}
}
[/as]
then, (instead of typing that in each of your wanna-be dispatching classes), just like AS3, you extend it:
[as]
import AS2EventDispatcher;
class DataModel extends AS2EventDispatcher{
public function DataModel(){
super();//only drawback... in AS2 the constructor must be created and super must be invoked
}
private function dispatchSomethingSweet():Void{
dispatchEvent(new Event("something_sweet"));
}
}
[/as]
as the discerning reader would have noticed (kl3ver boy), new Event(”some_event”) is not standard AS2… so, this event class must also be created, in order to be extended:
[as]
class events.Event{
private var _type:String;
public function set type(t:String):Void{}
public function get type():String{return _type;}
private var _target:Object;
public function set target(t:Object):Void{_target=t;}
public function get target():Object{return _target;}
public function Event(t:String,targ:Object){
_type=t;
target=targ;
}
public function toString():String{
return "Event{_type:"+_type+"_target:"+_target+"}";
}
public function clone():Event{
return new Event(_type,_target);
}
}[/as]
why one might want to clone() events in as2 is beyond my willingness to think about the subject though…
anyhow, good times, good times…

July 5th, 2007 at 7:42 am
Hello! Good Site! Thanks you! jrinasbvlwnt
July 5th, 2007 at 10:31 am
thank you Btzghdmaeo, you’ve made my day!
However, I’m not sure I fully understand when you say “jrinasbvlwnt”, do you mean it in a derogatory manner? sounds rather hostile?
thanks again!
kthxbye
May 6th, 2008 at 3:35 pm
but the clunky way to “shove in” the code of EventDispatcher allowed us to shove it right into classes extending MovieClip. How can I use your attempt for that? In AS3 MovieClip already extends EventDispatcher, but in AS2 it does NOT. Any ideas?
May 6th, 2008 at 4:16 pm
HI Mattias,
I guess you could extend MovieClip with a EventDispatchingMovieClip like so:
import mx.events.EventDispatcher;
class EventDispatchingMovieClip extends MovieClip{
public function EventDispatchingMovieClip(){
EventDispatcher.initialize(this);
}
public function dispatchEvent(){}
public function addEventListener(){}
public function removeEventListener(){}
}
(sorry about the formatting, I thought I had a code formatting plugin but it seems to be malfunctioning
).
Then, whenever you need dispatching, instead of extending MovieClip, extend EventDispatchingMovieClip… Purists prefer composition over inheritance, but let’s face it, this was a hack to begin with
May 7th, 2008 at 2:03 pm
thank you!
July 18th, 2008 at 3:15 am
Cheers for the class mate, very nice. It alleviates a lot of the bitchy aspects of the EventDispatcher.initialize(this) method that caused me much pain in the past. Anyhow, how would you propose I pass data along with the event? Should I make a custom event class as in AS3? Thanks again…
July 18th, 2008 at 3:48 am
Nevermind! Silly question…don’t know what I was thinking. I didn’t think that I can just pass an object to the constructor!