Using event handler methods 331
For example, suppose you have a button named next_btn on the Stage. The following code
assigns a function to the button’s
onPress event handler; this function advances the playhead
to the next frame in the current timeline:
next_btn.onPress = function () {
nextFrame();
}
Assigning a function reference
In the previous code, the nextFrame() function is
assigned to an event handler for
onPress. You can also assign a function reference (name) to
an event handler method and later define the function, as shown in the following example:
// Assign a function reference to button's onPress event handler.
next_btn.onPress = goNextFrame;
// Define goNextFrame() function.
function goNextFrame() {
nextFrame();
}
Notice in the following example that you assign the function reference, not the function’s
return value, to the
onPress event handler:
// Incorrect!
next_btn.onPress = goNextFrame();
// Correct.
next_btn.onPress = goNextFrame;
Receiving passed parameters
Some event handlers receive passed parameters that provide
information about the event that occurred. For example, the
TextField.onSetFocus event
handler is invoked when a text field instance gains keyboard focus. This event handler receives
a reference to the text field object that previously had keyboard focus.
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";
};