344 Handling Events
For example, consider the following two event handlers. The first is an onPress event handler
associated with a movie clip named
clip_mc. The second is an on() handler attached to the
same movie clip instance.
// Attached to clip_mc's parent clip timeline:
clip_mc.onPress = function () {
var shoeColor; // local function variable
shoeColor = "blue";
}
// on() handler attached to clip_mc:
on (press) {
var shoeColor; // no local variable scope
shoeColor = "blue";
}
Although both event handlers contain the same code, they have different results. In the first
case, the
color variable is local to the function defined for onPress. In the second case,
because the
on() handler doesn’t define a local variable scope, the variable is defined in the
scope of the timeline of the
clip_mc movie clip.
For
on() event handlers attached to buttons, rather than to movie clips, variables (as well as
function and method calls) are invoked in the scope of the timeline that contains the
button instance.
For instance, the following
on() event handler produces different results that depend on
whether it’s attached to a button or movie clip object. In the first case, the
play() function
call starts the playhead of the timeline that contains the button; in the second case, the
play() function call starts the timeline of the movie clip to which the handler is attached.
// Attached to button.
on (press) {
play(); // Plays parent timeline.
}
// Attached to movie clip.
on (press) {
play(); // Plays movie clip's timeline.
}
When attached to a button object, the play() function applies to the timeline that contains
the button—that is, the button’s parent timeline. But when the
on(press) handler is
attached to a movie clip object, the
play() function call applies to the movie clip that bears
the handler. If you attach the following code to a movie clip, it plays the parent timeline:
// Attached to movie clip.
on (press) {
_parent.play(); // Plays parent timeline.
}