This chapter is from the book

Creating graphics and animation in Flash is only half the story. The other half is interactivity, which involves giving the viewer control of those graphics and animation via buttons, the keyboard, and the mouse. Interactivity is essential for basic site navigation and interfaces on the Web, as well as for game development, online tutorials, or anything else that requires the viewer to make choices.

What makes a movie interactive? Interactivity is the back-and-forth communication between the user and the movie. In a Flash movie, the user might react to something that’s going on by moving the pointer, clicking the mouse button, or pressing a key on the keyboard. That reaction may trigger a response from the Flash movie, which in turn prompts the user to do something else. The things that the user does—mouse movements, button clicks, or keyboard presses—are part of things that happen, called events. Events form the basis of interactivity. There are many kinds of events—some are user driven whereas others are not. You’ll learn to make Flash listen for these events and respond to them. This whole process is known as event handling.

This chapter first introduces events, listeners, and functions used to respond to events. Next, it explores the simplest class for creating interactivity: the SimpleButton class. You’ll learn how to extend its functionality by creating invisible buttons, animated buttons, and more complex buttons, such as a pull-down menu. You’ll also learn about the classes and events that are involved in keyboard input and the context menu. Additionally, you’ll learn an important event known as the ENTER_FRAME event, which you’ll rely on to create continuously running actions. Understanding these classes and event handling is essential to creating Flash interactivity because these elements are the scaffold on which you’ll hang virtually all your ActionScript.

Listening for Events

Events are things that happen that Flash can recognize and respond to. A mouse click is an event, as are mouse movements and keypresses on the keyboard. Events can also be things that the user doesn’t initiate. The completion of a sound, for example, is an event. Anytime an event happens, an object of the Event class is created. When the mouse button is clicked, a MouseEvent object (a subclass of the Event class) is created. When a key on the keyboard is pressed, a KeyboardEvent object (another subclass of the Event class) is created. It may seem a little strange that an object represents an event, but remember Flash objects can be very abstract!

With all these events happening, you need a way to detect and respond to them. You detect an event by creating an event handler. An event handler simply tells Flash what to do when a specific kind of event happens. Creating an event handler is a two-part operation: first, you add a listener to detect the event and trigger a function, and second, you create the function that tells Flash how to respond. (It actually doesn’t matter if you create the listener first or the function first. As long as they are both in the same block of code, the event handler will work.)

For example, if you want to listen for a mouse click on top of a particular button, you add an event listener to that object as follows:

myButton_btn.addEventListener (MouseEvent.CLICK, reportClick);

The addEventListener() method takes two parameters. The first is the specific kind of event that you want to detect. All the event objects have properties (like MouseEvent.CLICK), which give more specificity to the event. The second parameter is the name of your function, which is triggered when the event is detected.

Next, add a function as the response to the event. Create the function with a parameter strictly typed to the MouseEvent object, like so:

In between the curly braces of the function, you add actions as the response. The word myevent in this example is the parameter name that you make up that refers to the event.

The actual object that receives the event (in this example, it is the button called myButton_btn) can be referenced in the function by using the property target. In the preceding example, the expression myevent.target references myButton_btn.

When you no longer need to listen for an event, you can delete the listener with the method removeEventListener(). The method takes two parameters, which are identical to the ones in the addEventListener() method.

Event Flow

Event handling is a little more involved than what is described here. When an event occurs and an Event object is created, the Event object systematically moves through other objects on the Flash Stage in a process known as the event flow. There are three parts to the event flow: a capture phase, a target phase, and a bubbling phase. Imagine that a mouse click happens on a button that is inside a movie clip on the Stage (Figure 4.1). The MouseEvent object is created, is dispatched from the Stage, and flows down to the movie clip and to the button inside the movie clip. That downward flow through those objects is the capture phase. The target phase involves the time the MouseEvent object is at the target (the button). Then the MouseEvent object proceeds to bubble, or flow, up the hierarchy to the main Stage (Figure 4.2). This round-trip flow is important because it lets you put a listener at any point along its path and still detect the event. In other words, the listener doesn’t have to be tied to the object where the event occurs.

Figure 4.1 Events traverse the display list, which are the objects on the Stage. This example shows the main Stage with a movie clip on it. Inside the movie clip is a button, where a mouse click occurs.

Figure 4.2 When a mouse click occurs on a target (shown here as the button), a MouseEvent is dispatched and travels from the Stage down to the event target, and then bubbles upward back to the Stage. Listeners are usually put on the event target, but it is not required. For example, a listener could be put on the movie clip, and it would detect events happening on the movie clip or on objects inside the movie clip.

However, many events don’t proceed through all three phases of the event flow. Some events, such as the Event.ENTER_FRAME object, are dispatched directly to the target object and don’t participate in a capture or bubbling phase.