227
Chapter 12: Handling events
An event-handling system allows programmers to respond to user input and system events in a convenient way. The
ActionScript™ 3.0 event model is not only convenient, but also standards-compliant, and well integrated with the
Adobe® Flash® Player 9 and Adobe® AIR™ display lists. Based on the Document Object Model (DOM) Level 3 Events
Specification, an industry-standard event-handling architecture, the new event model provides a powerful yet
intuitive event-handling tool for ActionScript programmers.
This chapter is organized in five sections. The first two sections provide background information about event
handling in ActionScript. The last three sections describe the main concepts behind the event model: the event flow,
the event object, and event listeners. The ActionScript 3.0 event-handling system interacts closely with the display
list, and this chapter assumes that you have a basic understanding of the display list. For more information, see
“Display programming” on page 247.
Contents
Basics of handling events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
How ActionScript 3.0 event handling differs from earlier versions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
The event flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
Event objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
Event listeners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
Example: Alarm Clock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
Basics of handling events
Introduction to handling events
You can think of events as occurrences of any kind in your SWF file that are of interest to you as a programmer. For
example, most SWF files support user interaction of some sort—whether it's something as simple as responding to
a mouse click or something more complex, such as accepting and processing data entered into a form. Any such user
interaction with your SWF file is considered an event. Events can also occur without any direct user interaction, such
as when data has finished loading from a server or when an attached camera has become active.
In ActionScript 3.0, each event is represented by an event object, which is an instance of the Event class or one of its
subclasses. An event object not only stores information about a specific event, but also contains methods that facil-
itate manipulation of the event object. For example, when Flash Player or AIR detects a mouse click, it creates an
event object (an instance of the MouseEvent class) to represent that particular mouse click event.
After creating an event object, Flash Player or AIR dispatches it, which means that the event object is passed to the
object that is the target of the event. An object that serves as the destination for a dispatched event object is called an
event target. For example, when an attached camera becomes active, Flash Player dispatches an event object directly
to the event target, which in this case is the object that represents the camera. If the event target is on the display list,
however, the event object is passed down through the display list hierarchy until it reaches the event target. In some
cases, the event object then “bubbles” back up the display list hierarchy along the same route. This traversal of the
display list hierarchy is called the event flow.

Get ADOBE® FLEX® 3: PROGRAMMING ACTIONSCRIPT™ 3.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.