Handling Events
ActionScript 3.0 and the Flex framework use events to notify and receive notification when things occur. Events occur in response to the user (e.g., the user clicks on something), time (timer events), and asynchronous messaging (such as remote procedure calls). Regardless of the cause of an event, nearly all ActionScript events use the same event model.
In Chapter 3, you saw how to use event handler attributes. In ActionScript, you can handle events by registering listeners. A listener is a function or method that should receive notifications when an event is dispatched. For example, you can register a method to receive a notification when the user clicks a button.
You need at least two elements to register a listener: an object
that dispatches events, and a function that listens for events. Objects capable of
dispatching events either extend the flash.events.EventDispatcher class or implement the flash.events.IEventDispatcher
interface. When an object can dispatch events, it has a public addEventListener() method
that requires at least two parameters: the name of the event for which you
want to listen and the function/method that should listen for the
event.
object.addEventListener("eventName", listenerFunction);In most cases, the event names are stored in
constants of the corresponding event type class. For example, the
click event name is stored in the MouseEvent.CLICK constant.
The listener function must expect one parameter of type mx.events.Event or the relevant ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access