onClipEvent (enterFrame) {
trace(x); // Displays the value of navigation.x
}
The interpreter does not consult a local scope first because there is
no local scope to consult. If we write var y =
10; in our handler, y is defined on
navigation's timeline, even though the
var keyword ordinarily declares a local variable
when used in a function.
The easiest way to remember the scope rules of a clip event handler
is to treat the handler's statements as though they were
attached to a frame of the handler's clip. For example, suppose
we have a clip named ball that has a variable
called xVelocity in it. To access
xVelocity from inside a ball
event handler, we simply refer to it directly, like this:
onClipEvent (mouseDown) {
xVelocity += 10;
}
We don't have to supply the path to the variable as
_root.ball.xVelocity because the interpreter
already assumes we mean the variable xVelocity in
ball. The same is true of properties and methods;
instead of using ball._x, we simply use
_x, and instead of using
ball.gotoAndStop(5), we simply use
gotoAndStop(5). For example:
onClipEvent (enterFrame) {
_x += xVelocity; // Move the ball
gotoAndPlay(_currentframe - 1); // Do a little loop
}
We can even define a function on ball using a
function declaration statement in a handler, like this:
onClipEvent (load) {
function hideMe( ) {
_visibility = 0;
}
}
It's sometimes easy to forget that statements in clip event
handlers are scoped to the clip's
timeline, not the handler function's local scope and
not the clip's parent timeline (the
timeline upon which the clip resides).
For example, suppose we place our ball clip on the
main timeline of a movie, and the main timeline (not
ball's timeline) has a moveBall(
) function defined on it. We may absent-mindedly call
moveBall( ) from an event handler on
ball like this:
onClipEvent (enterFrame) {
moveBall( ); // Does nothing! There's no moveBall( ) function in ball.
// The moveBall( ) function is defined on _root
}
We have to explicitly refer to the moveBall( )
function on the main timeline using _root like
this:
onClipEvent (enterFrame) {
_root.moveBall( ); // Now it works!
}
this._x // Same as next line
_x
this.gotoAndStop(12); // Same as next line
gotoAndStop(12);
Use of this is most frequently required when
we're dynamically generating the name of one of the current
clip's properties (either a variable name or a nested clip).
Here we tell one of the nested clips in the series
ball.stripe1, ball.stripe2, . .
. to start playing, depending on the current frame of the
ball clip:
onClipEvent (enterFrame) {
this["stripe" + _currentframe].play( );
}
The keyword this is also frequently used with
movie clip methods that demand an explicit reference to a movie clip
object upon invocation. Any movie clip method with the same name as
an ActionScript global function must be used with an explicit clip
reference. The this keyword is therefore necessary
when invoking the following functions as methods inside an event
handler:
duplicateMovieClip( )
loadMovie( )
loadVariables( )
print( )
printAsBitmap( )
removeMovieClip( )
startDrag( )
unloadMovie( )
For example:
this.duplicateMovieClip("ball2", 1);
this.loadVariables("vars.txt");
this.startDrag(true);
this.unloadMovie( );