ActionScript 3 uses the EventDispatcher (flash.events.EventDispatcher) class for all event handling. This class was available in ActionScript 2, but it existed as an external class in the mx framework. Now, it is built into the player (and in being so, improves performance).
Whenever you want to create an event handler to be called during a certain event, whether it be every frame (enterFrame event), or at the press of a button (mouseDown event), in AS 3, you will need to use EventDispatcher. This means there are no more onEnterFrame of onPress functions you can define that will automatically handle these events, nor are there any simple addListener methods for generalized event listening. EventDispatcher and addEventListener (and related methods) does it all.
Methods
- addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void
- dispatchEvent(event:Event):Boolean
- hasEventListener(type:String):Boolean
- removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void
- willTrigger(type:String):Boolean
Note that addEventListener only takes functions as listeners, not objects. Also remember that class methods are bound to their instances so when used as listeners, 'this' in the event call still references the orginal class instance no matter what object dispatched the event.
Basic Example:
ActionScript Code:
package {
import flash.display.Sprite;
import flash.events.Event;
public class MyDispatcher extends Sprite {
public function MyDispatcher() {
addEventListener("customEvent", handleEvent);
dispatchEvent(new Event("customEvent"));
}
private function handleEvent(event:Event):void {
trace(event.type); // "customEvent"
}
}
}
By extending the EventDispatcher class or any class that inherits from it like Sprite (all DisplayObjects are inherently EventDispatchers), your class gains access to the EventDispatcher methods. Then, other instances (or the class instance itself) can add methods to instances of that class using addEventListener and in turn they can call those events through dispatchEvent.
If there is some reason your class cannot inherit from the EventDispatcher class (for example, if its already inheriting from another class which does not inherit from EventDispatcher), then you can use the EventDispatcher constructor to intialize your class instance with the methods of EventDispatcher via aggregation (composition). Just make sure you implement the IEventDispatcher interface (flash.events.IEventDispatcher). Ex:
ActionScript Code:
package {
import flash.display.Sprite;
import flash.events.Event;
public class MyDispatcher extends Sprite {
public function MyDispatcher() {
var dispatcher:CustomDispatcher = new CustomDispatcher();
dispatcher.addEventListener("customEvent", handleEvent);
dispatcher.dispatchEvent(new Event("customEvent"));
}
private function handleEvent(event:Event):void {
trace(event.type); // "customEvent"
}
}
}
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.IEventDispatcher;
class CustomDispatcher implements IEventDispatcher {
private var eventDispatcher:EventDispatcher;
public function CustomDispatcher() {
eventDispatcher = new EventDispatcher(this);
}
public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void {
eventDispatcher.addEventListener.apply(null, arguments);
}
public function dispatchEvent(event:Event):Boolean {
return eventDispatcher.dispatchEvent.apply(null, arguments);
}
public function hasEventListener(type:String):Boolean {
return eventDispatcher.hasEventListener.apply(null, arguments);
}
public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void {
eventDispatcher.removeEventListener.apply(null, arguments);
}
public function willTrigger(type:String):Boolean {
return eventDispatcher.willTrigger.apply(null, arguments);
}
}
The CustomDispatcher helper class above doesn't inherit from EventDispatcher but uses aggregation to obtain EventDispatcher functionality through an instance of EventDispatcher initialized in the constructor.
0 评论:
发表评论