User Guide

Using event listeners 169
For example, the following code inserts some text into a text field that no longer has keyboard
focus:
this.createTextField("my_txt", 99, 10, 10, 200, 20);
my_txt.border = true;
my_txt.type = "input";
this.createTextField("myOther_txt", 100, 10, 50, 200, 20);
myOther_txt.border = true;
myOther_txt.type = "input";
myOther_txt.onSetFocus = function(my_txt:TextField) {
my_txt.text = "I just lost keyboard focus";
};
Event handlers for runtime objects
You can also assign functions to event handlers for objects
you create at runtime. For example, the following code creates a new movie clip instance
(
newclip_mc) and then assigns a function to the clips onPress event handler:
this.attachMovie("symbolID", "newclip_mc", 10);
newclip_mc.onPress = function () {
trace("You pressed me");
}
For more information, see “Creating movie clips at runtime” on page 211.
Overriding event handler methods By creating a class that extends an ActionScript 2.0 class,
you can override event handler methods with the functions that you write. You can define an
event handler in a new subclass that you can then reuse for various objects by linking any symbol
in the library of the extended class to the new subclass. The following code overrides the
MovieClip class
onPress event handler with a function that increases the transparency of the
movie clip:
// FadeColor class -- fades color when movie clip is clicked
class FadeColor extends MovieClip {
function onPress() {
this._alpha = 30;
}
}
For specific instructions on extending an ActionScript 2.0 class and linking to a symbol in the
library, see the examples in Assigning a class to a movie clip symbol” on page 218.
Using event listeners
Event listeners let an object, called a listener object, receive events broadcast by another object,
called a broadcaster object. The broadcaster object registers the listener object to receive events
generated by the broadcaster. For example, you can register a movie clip object to receive
onResize notifications from the Stage, or a button instance could receive onChanged
notifications from a text field object. You can register multiple listener objects to receive events
from a single broadcaster, and you can register a single listener object to receive events from
multiple broadcasters.