In the previous chapter, you learned how to use ActionScript to move, transform, and change parts of your animation. But when something happens is just as important as the action itself. For example, you may want a movie object to start playing as soon as the web page opens, or you may want to let your audience decide when to watch it. You use events to control the actions in your animation. It's as if your ActionScript program tells Flash: "When this (event) happens, do this (action)." The classic example is a button on the stage. The action statement says something like: "When this button gets clicked, go to Frame 25 of the movie clip, and then start playing." You provide the programming that puts the people who view your Flash creations in the driver's seat. Using events and event handlers, you can send your programs off into the world on their own, confident that they'll behave.
This chapter explains how to use ActionScript to detect events when they happen and how to get your animations to perform specific actions as a result. Unlike previous versions, ActionScript 3.0 has one single way of handling all kinds of events. So, once you learn the basics, you're all set to handle any event.
There are many different types of events. Some events—like a mouse click—are triggered by the people viewing your animation. Other events are simply occurrences in a Flash animation—like a movie clip reaching the last frame. The button is one of the easiest events to understand, and, not surprisingly, it's one of the most common. Someone clicks a button, and then an action takes place. To the person clicking, it appears that the button makes something happen. That's true to some extent, but there are some additional gears and levers behind the scenes.
The object related to the event is called the event target. So in the button example, the button is the event target. When an event like a button click happens, an event object is created. The job of this new event object is to deliver information to an event listener, which has instructions about what to do when that event happens. So, how does the event target know where to deliver the information carried within the event object? That detail is handled by registering the event. In effect, the code that registers an event says, "When this event happens, send that event object to this location." Figure 13-1 shows the major elements involved in event handling.
Figure 13-1. When an event related to the target happens, ActionScript sends a message, in the form of the event object, to the event listener, which performs the actions. You register specific events, like a mouse click, beforehand, so the target knows where to send the event object.
Like most things in ActionScript, an event is considered an object. Obviously, it's not an object in the sense that a rectangle or a circle on your stage is an object. It's an object by the strict ActionScript definition of an object because it has properties where it stores information about the event, and it has methods (functions) that let you perform actions related to the event. The Event class is the definition of events in a very general sense—similar to the class definition of a rectangle as a four-sided object. The Event class is the basis for other, more specific Event objects. Different types of events, such mouse events, keyboard events, and error events, have properties and methods that serve their specific needs.