Recipe 2.8.
Dispatching Events
Problem
You want to dispatch events.
Solution
Extend flash.events.EventDispatcher and
call the dispatchEvent( ) method.
Discussion
Events are an important way for objects to
communicate. They are essential for creating flexible systems.
Flash Player 9, for example, has a built-in event dispatching
mechanism in the flash.events.EventDispatcher class. All
classes that dispatch events inherit from EventDispatcher
(e.g., NetStream and Sprite). If you want to define a
class that dispatches events, you can extend
EventDispatcher, as follows:
package {
import flash.events.EventDispatcher;
public class Example extends EventDispatcher {
}
}
The EventDispatcher class has public
methods called addEventListener( ) and
removeEventListener( ) that you can call from any instance
of an EventDispatcher subclass to register event listeners.
EventDispatcher also defines a protected method called
dispatchEvent( ),
which you can call from within a subclass to dispatch an event. The
dispatchEvent( ) method requires at least one parameter as a
flash.events.Event
object or a subclass of Event.
|