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…