Event objects used with EventDispatcher in ActionScript 3 are a little less generic than those used with ActionScript 2. In AS 3, each event object has its own class. The most common event classes are Event (flash.events.Event) for general events and MouseEvent (flash.events.MouseEvent) for events associated with the mouse. Other event classes can be found in the flash.events package, all of which inherit from Event.
When using EventDispatcher.dispatchEvent(), you pass in an Event instance that is specific to the event being dispatched. The type of event dispatched is defined within the Event instance used. For example, dispatching an "enterFrame" event would mean using dispatchEvent with an Event instance instantiated with the type "enterFrame".
ActionScript Code:
dispatchEvent(new Event("enterFrame"));
When an event handler is executed as a result of this event, that event object is passed into the function as a single argument. Properties from that event can then be extracted to learn more about the event that occured. For example, the type property from the Event class tells which event was dispatched.
ActionScript Code:
addEventListener("enterFrame", eventHandler);
dispatchEvent(new Event("enterFrame"));
...
private function eventHandler(event:Event):void {
trace(event.type); // "enterFrame"
}
Though event types are strings, all common Flash events are available in Flash through static constants of the Event classes. The "enterFrame" event type, for example, is accessible using Event.ENTER_FRAME. Mouse events are within the MouseEvent class. Click events, for example, are MouseEvent.CLICK. These constants are prefered over their string representations.
ActionScript Code:
addEventListener(Event.ENTER_FRAME, eventHandler);
dispatchEvent(new Event(Event.ENTER_FRAME));
When making your custom Events, you can use the Event object with a custom type or alternatively, extend the Event class creating a new kind of Event instance to be used with your events.
1评论:
日历的问题英文原版我实验了一下,也出问题了……
但不知道为什么作者的博客是好的……
作者最近回复里也承认有很多错误待修正,耐心等作者修正以后我会尽快跟着修正的。
似乎最近blogger的归档本身也有问题,中文月份都变成英文了……
发表评论