< Day Day Up >
Using Event Handler Methods
All standard event handlers have equivalent event handler methods (also called callback
functions or function callbacks). For example:
on (press) is buttonName_btn.onPress or movieClipName_mc.onPress
on (release) is buttonName_btn.onRelease or movieClipName_mc.onRelease
on (enterFrame) is movieClipName_mc.onEnterFrame
In addition, these event handler methods exist but have no standard event equivalents:
Buttons/Movie Clips
nameOfClipOrButton.onKillFocus
nameOfClipOrButton.onSetFocus
Sound Objects
nameOfSoundObject.onLoad
nameOfSoundObject.onSoundComplete
nameOfSoundObject.onID3
Text Fields
nameOfTextField.onChanged
nameOfTextField.onKillFocus
nameOfTextField.onScroller
nameOfTextField.onSetFocus
Stage Objects
Stage.onLoad
StyleSheet Objects
nameOfStyleSheet.onResize
ContextMenu Objects
nameOfContextMenu.onSelect
ContextMenuItem Objects
nameOfContextMenuItem.onSelect
LoadVars Objects
nameOfLoadVarsObject.onLoad
SharedObject Objects
nameOfSharedObject.onStatus
LocalConnection Objects
nameOfLocalConnection.allowDomain
nameOfLocalConnection.onStatus
NetConnection Objects
nameOfNetConnection.onStatus
NetStream Objects
nameOfNetStream.onStatus
XML Objects
nameOfXMLObject.onData
nameOfXMLObject.onLoad
XMLSocket Objects
nameOfXMLSocketObject.onClose
nameOfXMLSocketObject.onConnect
nameOfXMLSocketObject.onData
nameOfXMLSocketObject.onXML
You can use numerous events to trigger a script. Because some of these objects are
intangible (for example, Sound, LoadVars, and XML), defining event handler methods
on a keyframe of the timeline is the only way to execute a script when an event occurs in
relation to that object (in contrast to buttons and movie clip instances, which you can
select on the stage and to which you can directly attach scripts).
NOTE
We will discuss and use many of these event handler methods throughout this book. For
more information, see the ActionScript dictionary.
By attaching a script to a button or movie clip instance using a regular event handler, you
pretty much lock down not only what happens when an event occurs but also the events
that actually trigger execution of a script. For example:
on (press) {
gotoAndPlay(5);
}
If you were to attach this script to a button, the button would react only to the press event,
performing a single action when that event occurred. To give you an idea of the power
and flexibility of event handler methods, assume there's a button instance on the stage
named myButton_btn. By placing the following script on Frame 1 of the main timeline
(assuming the button exists at that frame), you define how that button will react to certain
events:
myButton_btn.onPress = function() {
stopAllSounds();
}
myButton_btn.onRelease = function() {
myMovieClip_mc._xscale = 50;
}
When pressed, the button will halt all sounds; when released, it will horizontally scale
myMovieClip_mc to 50 percent of its original size.
However, by moving that timeline to Frame 2—which contains the following script
(assuming the button exists at Frame 2)—you would change the button's function
completely:
myButton_btn.onPress = null
myButton_btn.onRelease = null
myButton_btn.onRollOver = function() {
stopAllSounds();
}
myButton_btn.onRollOut = function() {
myMovieClip_mc._xscale = 50;
}