ActionScript 2.0 has no event system of its own in the language. It uses the mechanisms of Flash Player 6 and 7, handler properties and listener objects, and the event classes that ship with Flash as ActionScript 2.0 source. Handlers attached to instances in the FLA still work too.
Handlers in the FLA
on(event) on buttons and movie clips, and
onClipEvent(event) on movie clips, are written in the Actions panel on the
instance and saved in the FLA, never in a class file. Their events are those of
ActionScript 1.0: press, release,
releaseOutside, rollOver, rollOut,
dragOver, dragOut and keyPress for
on; load, unload, enterFrame,
mouseDown, mouseUp, mouseMove,
keyDown, keyUp and data for
onClipEvent. With classes, the same work is normally done by handler
methods instead.
Handler properties and methods
An object whose class has a method named after an event receives it. A movie clip
subclass simply defines onPress, onEnterFrame and the rest as
methods:
class com.example.Draggable extends MovieClip {
public function onPress():Void {
startDrag();
}
public function onRelease():Void {
stopDrag();
dispatchDrop();
}
public function onReleaseOutside():Void {
onRelease();
}
private function dispatchDrop():Void {
_parent.itemDropped(this, _droptarget);
}
}
| Object | Events | Player |
|---|---|---|
| MovieClip | onPress, onRelease, onReleaseOutside, onRollOver, onRollOut, onDragOver, onDragOut, onEnterFrame, onLoad, onUnload, onData, onMouseDown, onMouseUp, onMouseMove, onKeyDown, onKeyUp, onSetFocus, onKillFocus | 6 |
| Button | onPress, onRelease, onReleaseOutside, onRollOver, onRollOut, onDragOver, onDragOut, onSetFocus, onKillFocus | 6 |
| TextField | onChanged, onScroller, onSetFocus, onKillFocus | 6 |
| XML, LoadVars | onLoad(success), onData(text), and from player 8 onHTTPStatus(code) | 5, 6 |
| XMLSocket | onConnect, onXML, onData, onClose | 5 |
| Sound | onLoad, onSoundComplete, onID3 (player 7) | 6 |
| NetStream, NetConnection, SharedObject, LocalConnection, Camera, Microphone | onStatus(info), and for Camera and Microphone onActivity | 6 |
| ContextMenu, ContextMenuItem | onSelect | 7 |
A movie clip class’s onLoad method runs when an instance is created,
after the constructor; setting onLoad on a clip that already exists has no
effect.
Listener objects
| Broadcaster | Listener methods | Player |
|---|---|---|
Key.addListener(obj) | onKeyDown, onKeyUp | 6 |
Mouse.addListener(obj) | onMouseDown, onMouseUp, onMouseMove, and onMouseWheel(delta) on Windows | 6 |
Stage.addListener(obj) | onResize | 6 |
Selection.addListener(obj) | onSetFocus(oldFocus, newFocus) | 6 |
someTextField.addListener(obj) | onChanged, onScroller | 6 |
new MovieClipLoader().addListener(obj) | onLoadStart, onLoadProgress(target, loaded, total), onLoadComplete, onLoadInit, onLoadError(target, errorCode) | 7 |
AsBroadcaster.initialize(obj) gives any object the same
addListener, removeListener and
broadcastMessage(eventName, args…). It was undocumented in Flash MX, and
appears in Adobe’s later ActionScript 2.0 reference.
EventDispatcher
The version 2 components of Flash MX 2004 and their framework use
mx.events.EventDispatcher, a class shipped as source. It adds three methods
to an object or to a class’s prototype, and events are objects with at least a
type and a target:
import mx.events.EventDispatcher;
class com.example.Timer {
// filled in by EventDispatcher.initialize
public var addEventListener:Function;
public var removeEventListener:Function;
private var dispatchEvent:Function;
public function Timer() {
EventDispatcher.initialize(this);
}
public function finish():Void {
dispatchEvent({type: "complete", target: this});
}
}
// elsewhere
var t:Timer = new Timer();
t.addEventListener("complete", Delegate.create(this, onTimerDone));
A listener can be a function, an object with a method named after the event type, or
an object with a handleEvent method. The component events, such as a
Button’s click or a List’s change, are received this way. The
declarations of the three function members are only there to satisfy the compiler.
Delegate
Handlers and listeners run with this set to the object that calls them.
mx.utils.Delegate.create(scope, method), from the Flash MX 2004 7.2 update,
returns a function that calls method with this set to
scope, so a class can pass its own methods as handlers; see
Functions.
Timers
| Call | Does | Player |
|---|---|---|
setInterval(f, ms, args…), setInterval(obj, "method", ms, args…) | Call repeatedly; return an ID | 6 |
clearInterval(id) | Stop | 6 |
setTimeout(f, ms, args…), clearTimeout(id) | Call once after a delay, or cancel it | 8 |
getTimer() | Milliseconds since the movie started | 4 |
For players before Flash Player 8, a single delayed call is made with
setInterval, cleared on its first call.