Macromedia Flash MX 2004 ActionScript Programming Tutorial

Event Handling

Event handling is a fancy way of describing how interaction is produced. All interaction is composed of a cause and a behavior. The behavior happens as a result of the cause—what you tell Flash to produce the behavior. This cause is the event.

In the last section, you added an event handler to a button that caused the animation to play. In simple terms, you told Flash that the cause—the event—was the release of the mouse button (on(release)) and the resulting behavior was play.

Any number of things in Flash can cause a behavior. It can be caused by a user’s interaction with a button (such as clicking it or rolling over it), a user’s keystroke (when the user presses a certain key on the keyboard), or a system event, like the timeline entering a frame. When you added the stop action to the first and last keyframes of the animation in the last section, those actions, or behaviors, were caused by the timeline entering those frames. Because the timeline automatically moves from frame to frame, you don’t need to explicitly state the event causing the stop action—as soon as Flash encounters the action in the frame, it will execute the action. But most actions need to be placed in event handlers, so Flash knows when to execute them.

You add the code for event handlers to whatever object is producing the action. For example, if a button is being clicked to produce the action, the event handler must be added to the button, and not to a frame on the main timeline. When adding event handlers, you add the event in parentheses and the resulting behaviors, or actions, between curly braces, as shown below:

The on event handler is used to detect mouse, keyboard and movie clip events. These events—which include click, press, release, rollover and keyPress—trigger user-initiated actions. For example, the code in the screenshot above, which is attached to a movie clip instance named bad_driving_mc, causes the startDrag action to occur when the user presses the mouse button (on (press)) and the stopDrag action to occur when the user releases the mouse button (on (release)). This code lets the user click and drag a movie clip.

Similarly, the following code triggers a gotoAndPlay action when the user presses Enter on the keyboard:

on (keyPress “<Enter>”) {




Enter, Esc (Escape), and arrow keys (Left, Right, etc.) must be enclosed in the less-than (<) and greater-than (>) symbols. Other keys are only enclosed in quotation marks. KeyPress events are also case-sensitive, and multiple key combinations are not supported.

Button, movie clip, and keyboard (keyPress) events are attached to button and movie clip instances on the stage, since it’s the user’s interaction with these objects that trigger the events. You can combine events in one event handler by including both events in the parentheses, separated by a comma. Below, the on event handler is used to trigger an action when the user either clicks the button or presses Enter:

on (release, keyPress “<Enter>”) {




Although a keyPress event can occur at any time in the movie, it still must be attached to a specific button instance. One way around this problem is to create an invisible button to attach the code to.


Thanks to Flash’s Intellisense, it isn’t necessary to remember all the possible events. When you add an event handler to the Script pane, Flash opens a menu that displays the available events:

Flash MX 2004 includes some new events; for example, the click event has been added for use with Flash’s User Interface components.


The User Interface components are actually compiled movie clips, so don’t confuse the Button component with a button symbol.

Some event handlers are, or can be, system-initiated or time-based. The onClipEvent handler detects movie clip events that are either user-initiated, like mouseDown, mouseUp and keyDown, or that are triggered when the movie clip loads (load) or enters a frame (enterFrame). You can see a menu of onClipEvent events by typing the handler into the Script pane or selecting onClipEvent from the Movie Clip Control folder:

The onClipEvent(load) event happens once, when the movie clip is loaded into the movie. The onClipEvent(enterFrame) event happens every time a movie clip enters a frame in its timeline. Actions triggered by the enterFrame event are processed before any other actions that appear in a particular frame.

Because these events are specific to movie clips, the event handlers must be attached to specific movie clip instances. They cannot appear, for example, in a frame inside the movie clip. However, ActionScript 2.0 has implemented the MovieClip object, which makes it much easier to add movie clip actions, called methods, anywhere within the movie. We discuss this later in the tutorial, under Objects.

One Comment

  1. zoe

Leave a Reply

Your email address will not be published. Required fields are marked *