Event handler scope

The scope, or context, of variables and commands that you declare and execute within an event handler depends on the type of event handler you're using: event handlers or event listeners, or on() and onClipEvent() handlers.

Functions assigned to event handler methods and event listeners (like all ActionScript functions that you write) define a local variable scope, but on() and onClipEvent() handlers do not.

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 color; // local function variable
  color = "blue";
}
// on() handler attached to clip_mc:
on(press) {
  var color; // no local variable scope
  color = "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 scopes to the Timeline of the movie clip clip_mc.

For on() event handlers attached to buttons, rather than to movie clips, variables (as well as function and method calls) are scoped to the Timeline that contains the button instance.

For instance, the following on() event handler will produce different results depending on whether it's attached to a movie clip or button object. In the first case, the play() function call starts the playback head 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
}

That is, when attached to a button object, the play() method call applies to the Timeline that contains the button—that is, the button's parent Timeline. But when the same handler is attached to a movie clip object, then the play() applies to the movie clip that bears the handler.

Within an event handler or event listener function definition, the same play() function would apply to the Timeline that contains the function definition. For example, suppose the following MovieClip.onPress event handler function were declared on the Timeline that contains the movie clip instance myMovieClip.

// Function defined on movie clip Timeline:
myMovieClip.onPress = function () {
  play(); // plays Timeline that contains the function definition
}

If you want to play the movie clip that defines the onPress event handler, then you have to refer explicitly to that clip using the this keyword, as follows:

myMovieClip.onPress = function () {
  this,play(); // plays Timeline of clip that defines the onPress handler
}

For more information about the scope of the this keyword in event handlers, see Scope of the "this" keyword.