Accessibility

Table of Contents

Introduction to event handling in ActionScript 3.0

Event targeting

All events, like mouse clicks, start off in Flash with the process of event targeting. This is the process by which Flash determines which object is the target of the event (where the event originates). In the previous examples we've seen how Flash was able to determine whether or not you clicked on the box or the stage. For every mouse event, the event references one object—the object of highest arrangement capable of receiving the event (see Figure 5).

Event targeting when clicking in the Flash movie (through Flash Player)

Figure 5. Event targeting when clicking in the Flash movie (through Flash Player)

This behavior is almost the same as that seen in ActionScript 2.0. For example, in ActionScript 2.0, when clicking once on two buttons that are placed directly on top one another (assuming they both have event handlers assigned to them), only the topmost button will receive the event. This remains to be the case with ActionScript 3.0. Though both buttons are technically below the mouse during the click, only the topmost button receives the event because that is the object which is targeted for the event.

There is one difference with ActionScript 3.0: All display objects are, by default, enabled to receive mouse events. This means that even if no event handlers have been assigned to a particular display object, it will still be targeted for an event when clicked, preventing anything below it from receiving events. This is not the case with ActionScript 2.0. With ActionScript 2.0, movie clip instances are enabled to receive events only when an event handler like onPress is assigned to them. Without such a handler, the instances are ignored by event targeting and those beneath can be targeted instead.

To get this behavior in ActionScript 3.0, use the InteractiveObject mouseEnabled property. Setting it to false will disable an interactive object instance from receiving mouse events and allows other instances below it to be targeted for mouse events:

myInteractiveObject.mouseEnabled = false;

Event objects

Event objects are the objects listener functions receive as an argument when called during the occurrence of an event. Event objects in ActionScript 3.0 are similar to those used in the ActionScript 2.0 version of the EventDispatcher class. The main difference is that now they are a little more structured by having their own class (the Event class) and have additional properties to describe the event being handled.

The event objects received by listener functions are always of the type Event but can also be a subclass of Event rather than specifically being an Event instance. Common subclasses include MouseEvent for events associated with the mouse and KeyboardEvent for events associated with the keyboard. Each class also contains the event type constants used for listening to related events, e.g. MouseEvent.CLICK.

The stage

The stage is a rather new concept in ActionScript 3.0, at least in terms of the stage being an object that can be referenced in code. The stage represents the topmost container of all display objects within a Flash movie, almost an equivalent of _root in ActionScript 2.0. But with ActionScript 3.0, the root (accessed via the DisplayObject root property, without an underscore) now exists within the stage object.

Additionally, stage targeting for mouse events does not depend on stage contents as is the case with other objects. With the box example, you have the basic hierarchy of stage > root > box (in Flex the hierarchy is stage > root > application > box). To click on the box and have Flash target the box for the click event, you need to click on the shape that makes up the box. Similarly, to click on the root object, you need to click on its contents, or the box instance. Clicking anywhere else will not be clicking on root since root consists only of the box. For the stage, however, you can click on the stage by clicking anywhere on the movie because the stage exists everywhere as a persistent background for the movie, even if there is no other content on the stage. In some cases (with mouse up and mouse move events for instance), the stage can even detect interaction outside of the movie. This behavior will be important when trying to achieve some tasks in ActionScript 3.0, as we will see.