
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Event Handler Scope
|
235
which the button resides. When attached to a movie clip, an on( ) handler is scoped
to the clip that bears the handler, as with an onClipEvent( ) handler. Prior to Flash
MX, movie clips did not support on( ) handlers (only Flash 5 buttons did).
For example, if we place a button on the main timeline and declare the variable
speed
in a handler on that button, speed will be scoped to the main timeline ( _root):
// CODE FOR BUTTON HANDLER
on (release) {
var speed = 10; // Defines speed on _root
}
In contrast, if we place an on( ) handler on a movie clip object named ball and
declare the variable
speed in the handler, speed is scoped to ball:
// CODE FOR ball HANDLER
on (release) {
var speed = 10; // Defines speed on ball, not _root
}
Inside an on( ) handler for a button object, the this keyword refers to the timeline on
which the button resides:
// CODE FOR BUTTON HANDLER
on (release) {
// Make the clip on which this button resides 50% transparent
this._alpha = 50;
// Move the clip on which this button resides 10 pixels to the right
this._x += 10;
}
But inside a movie clip’s on( ) handler, the this keyword refers to the clip that bears
the handler:
// CODE FOR MOVIE CLIP HANDLER
on (release) {
// Make the clip with the handler 50% transparent
this._alpha = 50;
// Move the ...