
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
232
|
Chapter 10: Events and Event Handling
Scope of onClipEvent( ) Handlers
Unlike event handler properties and event listener methods, on( ) and onClipEvent( )
handlers do not define a local scope! When we attach an onClipEvent( ) handler to a
clip, the scope of the handler is the clip, not the event handler. This means that all
variables are retrieved from the clip’s timeline. For example, if we attach an
onClipEvent(enterFrame) handler to a clip named
navigation and write trace(x);
inside the handler, the interpreter looks for the value of
x on navigation’s timeline:
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 the onClipEvent( ) handler,
y is defined on
navigation’s timeline, even though the var keyword ordinarily declares a local vari-
able when used in a function.
The easiest way to remember the scope rules of an onClipEvent( ) 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 on the main timeline that has a
variable called
xVelocity in it. To access xVelocity from inside ball’s onClipEvent( )
handler, we simply refer to it directly, ...