Figure 10-1. Attaching an event handler to a button
Let's try making a simple event handler function for both a
button and a movie clip. To create a button event handler, follow
these instructions:
Start a new Flash movie.
Create a button and drag an instance of it onto the main Stage.
With the button selected, type the following code in the Actions
panel:
on (release) {
trace("You clicked the button");
}
Select Control
Test Movie.
Click the button. The message, "You clicked the button,"
appears in the Output window.
When the movie plays and we press and release the button, the
release event is detected by the interpreter and
it executes the on (release) event handler. Each
time that we press and release the button, the message, "You
clicked the button," appears in the Output window.
Now let's try making a slightly more interesting event handler
on a movie clip. Once again, follow the instructions:
Start a new Flash movie.
On the main movie Stage, draw a rectangle.
Select Insert
Convert to Symbol.
In the Symbol Properties dialog box, name the new symbol
rectangle and select Movie Clip as the Behavior.
Click OK to finish creating the rectangle movie
clip.
Select the rectangle clip on stage, and then type
the following in the Actions panel:
onClipEvent (keyDown) {
_visible = 0;
}
onClipEvent (keyUp) {
_visible = 1;
}
Select Control
Test Movie.
Click the movie to make sure it has keyboard focus, then press and
hold any key. Each time you depress a key, the
rectangle movie clip disappears. Each time you
release the depressed key, rectangle reappears.
Notice that we don't manually issue any handler-invocation
statements -- the interpreter automatically invokes our event
handler when the corresponding event occurs.
Flash doesn't support attaching and removing handlers via
ActionScript while the movie is playing. Event handlers must be
assigned to buttons and movie clips using the Flash authoring tool.
The following imaginary syntax, therefore, is not legal:
myClip.onKeyDown = function ( ) { _visible = 0; };