User Guide

55
CHAPTER 4
Handling Component Events
Every component has events that are broadcast when a user interacts with it (for example, the
click and change events) or when something significant happens to the component (for
example, the
load event). To handle an event, you write ActionScript code that executes when the
event occurs.
Each component broadcasts its own set of events. This set includes the events of any class from
which the component inherits. This means that all components, except the media components,
inherit events from the UIObject and UIComponent classes, because they are the base classes of
the version 2 architecture. To see the list of events a component broadcasts, see the component’s
entry and its ancestor classes’ entries in Chapter 6, “Components Dictionary,” on page 91.
This chapter uses several versions of a simple Macromedia Flash application, TipCalculator, to
teach you how to handle component events. The FLA and SWF files are installed with Flash MX
2004 version 7.2 to the Macromedia/Flash MX 2004/Samples/HelpExamples/TipCalculator
folder.
This chapter contains the following sections:
Using the on() event handler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Using listeners to handle events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Delegating events. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
About the event object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Using the on() event handler
The easiest, but least powerful, way to handle a component event is to use the on() event handler.
You can assign the
on() event handler to a component instance, just as you would assign a
handler to a button or movie clip. For complex applications, it’s best to use event listeners. For
more information, see “Using listeners to handle events” on page 56.