User Guide

How ActionScript 3.0 event handling differs from earlier versions 347
How ActionScript 3.0 event handling
differs from earlier versions
The most noticeable difference between event handling in ActionScript 3.0 and event
handling in previous versions of ActionScript is that in ActionScript 3.0 there is only one
system for event handling, whereas in previous versions of ActionScript there are several
different event handling systems. This section begins with an overview of how event handling
worked in previous versions of ActionScript, and then discusses how event handling has
changed for ActionScript 3.0.
Event handling in previous versions of ActionScript
Versions of ActionScript before ActionScript 3.0 provided a number of different ways to
handle events:
on() event handlers that can be placed directly on Button and MovieClip instances
onClipEvent() handlers that can be placed directly on MovieClip instances
Callback function properties, such as XML.onload and Camera.onActivity
Event listeners that you register using the addListener() method
The UIEventDispatcher class that partially implemented the DOM event model.
Each of these mechanisms presents its own set of advantages and limitations. The
on() and
onClipEvent() handlers are easy to use, but make subsequent maintenance of projects more
difficult because code placed directly on buttons and movie clips can be difficult to find.
Callback functions are also simple to implement, but limit you to only one callback function
for any given event. Event listeners are more difficult to implement—they require not only
the creation of a listener object and function, but also the registration of the listener with the
object that generates the event. This increased overhead, however, enables you to create several
listener objects and register them all for the same event.
The development of components for ActionScript 2.0 engendered yet another event model.
This new model, embodied in the UIEventDispatcher class, was based on a subset of the
DOM Events Specification, and developers who are familiar with component event handling
will find the transition to the new ActionScript 3.0 event model relatively painless.