AS3 101: Quick Tip – Dispatching Events Without Extending EventDispatcher
Tutorial Details
- Difficulty: Basix
- Platform: Flash (Flash Player 9+)
- Language: AS3
- Software used: Flash Professional CS3+, and a text editor
- Estimated Completion Time: Ten minutes
It’s rare, but it happens. Sometimes you absolutely need to extend a class, and that class does not already extend EventDispatcher. At the same time, you absolutely need your class to be an EventDispatcher. Because ActionScript 3 does not allow multiple inheritance, how do you reconcile this?
In other situations, perhaps you would like to create a class that has a bunch of static methods on it, and isn’t really meant to be instantiated. You would like to be able dispatch events from this class as well, but you can extend EventDispatcher because that enables instance methods, not class methods.
Let’s take a look at how to achieve these goals.
Step 1: How To Do It
The solution in both cases is to instantiate, hang onto, and use an EventDispatcher object. That is, a new EventDispatcher() object, not necessarily any of its subclasses. In the case of needing to extend something else, you can also have your class implement the IEventDispatcher interface.
The process goes something like this:
-
Implement
IEventDispatcher(not for the static class).package { import flash.events.*; public class ArrayDispatcher extends Array implements IEventDispatcher { public function ArrayDispatcher() { } } } -
Create a new
EventDispatcherobject, and store it in a property.private var _dispatcher:EventDispatcher public function ArrayDispatcher() { _dispatcher = new EventDispatcher(); } -
Implement the methods defined by
IEventDispatcher, and simply wrap around the matching method on yourEventDispatcherinstance.public function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void { _dispatcher.addEventListener(type, listener, useCapture, priority, useWeakReference); } public function dispatchEvent(event:Event):Boolean { return _dispatcher.dispatchEvent(event); } public function hasEventListener(type:String):Boolean { return _dispatcher.hasEventListener(type); } public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void { _dispatcher.removeEventListener(type, listener, useCapture); } public function willTrigger(type:String):Boolean { return _dispatcher.willTrigger(type); }
Step 2: A Static EventDispatcher
For reference, implementing this in a static class might look like this:
package {
import flash.events.*;
public class StaticDispatcher {
private static var _dispatcher:EventDispatcher = new EventDispatcher();
public static function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void {
_dispatcher.addEventListener(type, listener, useCapture, priority, useWeakReference);
}
public static function dispatchEvent(event:Event):Boolean {
return _dispatcher.dispatchEvent(event);
}
public static function hasEventListener(type:String):Boolean {
return _dispatcher.hasEventListener(type);
}
public static function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void {
_dispatcher.removeEventListener(type, listener, useCapture);
}
public static function willTrigger(type:String):Boolean {
return _dispatcher.willTrigger(type);
}
}
}
Note that there is no extends or implements, all members are now static, and the _dispatcher property is now created directly on the property, instead of in the constructor (because there is no constructor).
That’s All
Although quick to explain, this is definitely an advanced technique, and you probably won’t require it if you’re at a point where dispatching your own events is still a new concept. But file this away somewhere in your mind so you can come back to this example when you need it.

Note that it could be useful to pass “this” to the constructor of EventDispatcher, in order to reference the dispatching Object (of ArrayDispatcher) as the target of the dispatched Event.
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/events/EventDispatcher.html#EventDispatcher%28%29
good tutorial, as always.
Can you show an example how to use “ArrayDispatcher”, I’m not sure why you extend “Array”
Thanks
It’s just an example: Array doesn’t extend EventDispatcher, which means that ArrayDispatcher – which extends Array – won’t extend EventDispatcher either.
just a small comment…
Static is BAD!
Static is not bad is every situation. Don’t get into the mindset that global is always incorrect, static properties and constants are used frequently by Adobe themselves in ActionScript’s built in classes. Every time you use Event.ENTER_FRAME you are using a static constant.
It is true that you must be very careful with global data and it can create a mess but if it were bad in every conceivable situation then Adobe wouldn’t have put it in the language in the first place!
Well said – good attitude :)
In case it might be helpful, If you make the class [Bindable] it will automatically implement IEventDispatcher. To verify this you can take a look at the generated actionsript.