For example, suppose you have a button named
assigns a function to the button's
the next frame in the Timeline.
next_btn.onPress = function ()
nextFrame();
}
In the above code, the
a function reference (name) to an event handler method and then define the function later.
// Assign a function reference to button's onPress event handler method
next_btn.onPress = goNextFrame;
// Define doSubmit() function
function goNextFrame() {
nextFrame();
}
Notice that you assign the function reference, not the function's return value, to the
event handler.
// Incorrect!
next_btn.onPress = goNextFrame();
// Correct.
next_btn.onPress = goNextFrame;
Some event handlers receive passed parameters that provide information about the event that
occurred. For example, the
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 the text field that just lost keyboard focus.
userName_txt.onSetFocus = function(oldFocus_txt) {
oldFocus_txt.text = "I just lost keyboard focus";
}
The following ActionScript classes define event handlers: Button, ContextMenu,
ContextMenuItem, Key, LoadVars, LocalConnection, Mouse, MovieClip, MovieClipLoader,
Selection, SharedObject, Sound, Stage, TextField, XML, and XMLSocket. For more information
about the event handlers they provide, see these class entries in
Dictionary," on page
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 (
clip's
event handler.
onPress
_root.attachMovie("symbolID", "newclip_mc", 10);
newclip_mc.onPress = function () {
trace("You pressed me");
}
For more information, see
84
Chapter 4: Handling Events
event handler; this function advances the playhead to
onPress
function is assigned directly to
nextFrame()
TextField.onSetFocus
205.
"Creating movie clips at runtime" on page
on the Stage. The following code
next_btn
onPress
event handler is invoked when a text field
Chapter 12, "ActionScript
) and then assigns a function to the
newclip_mc
126.
. You can also assign
onPress
Need help?
Do you have a question about the FLASH MX 2004 - ACTIONSCRIPT and is the answer not in the manual?