A Flash movie is driven by events: frames being entered, the mouse and keys, and data
arriving. Flash 5 handled them with blocks attached to buttons and clips in the FLA.
Flash MX added handlers as function properties, which can be set from any script,
including an .as file, and listener objects.
on() on buttons
The script of a button instance in the FLA is made of on blocks, each
listing one or more events. Flash MX also allows them on movie clip instances, which
then act as buttons.
on (release, releaseOutside) {
_root.gotoAndStop("menu");
}
on (keyPress "<Enter>") {
_root.gotoAndStop("menu");
}
| Event | Happens when |
|---|---|
press | The mouse button goes down over the button. |
release | The mouse button comes up over the button, after a press on it. |
releaseOutside | The mouse button comes up away from the button, after a press on it. |
rollOver | The pointer moves onto the button. |
rollOut | The pointer moves off the button. |
dragOut | The pointer moves off the button with the mouse button held down after a press on it. |
dragOver | The pointer moves back onto the button with the mouse button still held down. |
keyPress "key" | The key is pressed, whether or not the button is under the pointer. The key is a single character, such as "a", or a name in angle brackets, such as "<Left>", "<Enter>" or "<Space>". |
Code in an on block runs in the scope of the timeline the button is on,
so this and unqualified names refer to that timeline, not to the
button.
onClipEvent() on movie clips
The script of a movie clip instance in the FLA is made of onClipEvent
blocks, each for one event. The code runs in the scope of the clip itself.
| Event | Happens when |
|---|---|
load | The clip appears on the timeline, once. |
unload | In the first frame after the clip is removed. |
enterFrame | Every frame, at the movie’s frame rate, before the frame scripts of that frame. |
mouseMove | The mouse moves anywhere; _xmouse and _ymouse give its position. |
mouseDown, mouseUp | The left mouse button goes down or up, anywhere on the movie. |
keyDown, keyUp | A key goes down or up; Key.getCode() gives which. |
data | Data loaded by loadVariables has all arrived, or a part of a movie loaded by loadMovie has arrived. |
The mouse and key events reach every clip that has a handler for them, not only the
one under the pointer. Calling updateAfterEvent() in a mouse or key
handler redraws the screen at once instead of at the next frame.
onClipEvent (load) {
speed = 4;
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.RIGHT)) _x += speed;
if (Key.isDown(Key.LEFT)) _x -= speed;
}
speed and
_x belong to the clip.Handler properties (Flash MX)
From Flash Player 6 an event handler can be a function stored in a property of the
object, set and replaced by any script. Setting one of the button events on a movie
clip makes it act as a button, with the hand pointer. Setting the property to
null, or deleting it, stops the handler.
| Object | Handler properties |
|---|---|
| MovieClip | onPress, onRelease, onReleaseOutside, onRollOver, onRollOut, onDragOver, onDragOut, onEnterFrame, onLoad, onUnload, onData, onMouseDown, onMouseUp, onMouseMove, onKeyDown, onKeyUp, onSetFocus, onKillFocus |
| Button | onPress, onRelease, onReleaseOutside, onRollOver, onRollOut, onDragOver, onDragOut, onSetFocus, onKillFocus |
| TextField | onChanged, onScroller, onSetFocus, onKillFocus |
| XML | onLoad(success), onData(text) (both from Flash 5) |
| XMLSocket | onConnect(success), onXML(doc), onData(text), onClose() (from Flash 5) |
| LoadVars | onLoad(success), onData(text) |
| Sound | onLoad(success), onSoundComplete() |
| SharedObject, LocalConnection, NetConnection, NetStream | onStatus(info), and for LocalConnection the methods named by the sender |
| Camera, Microphone | onActivity(active), onStatus(info) |
ball_mc.onEnterFrame = function () {
this._x += 4;
if (this._x > Stage.width) delete this.onEnterFrame;
};
var news = new XML();
news.ignoreWhite = true;
news.onLoad = function (success) {
if (success) showHeadlines(this.firstChild);
};
news.load("news.xml");
.as file.A clip’s onLoad set by a script after the clip exists comes too late
to run; it is useful on the prototype of a class registered with
Object.registerClass.
Listeners
Some objects tell any number of listener objects about events. A listener is any
object with methods named after the events; it is registered with
addListener and removed with removeListener.
| Broadcaster | Events sent to listeners |
|---|---|
Key | onKeyDown, onKeyUp |
Mouse | onMouseDown, onMouseUp, onMouseMove |
Stage | onResize, when the movie is not scaled to fit |
Selection | onSetFocus(oldFocus, newFocus) |
a TextField | onChanged, onScroller |
Any object can broadcast its own events with the AsBroadcaster object,
which Flash MX includes but does not document:
var scoreBoard = new Object();
AsBroadcaster.initialize(scoreBoard); // adds addListener, removeListener,
// broadcastMessage and _listeners
var display = new Object();
display.onScore = function (points) { score_txt.text = points; };
scoreBoard.addListener(display);
scoreBoard.broadcastMessage("onScore", 120); // calls display.onScore(120)
Timers
| Call | Does | Player |
|---|---|---|
setInterval(f, ms, arg1, …) | Calls a function every ms milliseconds, with the arguments given; returns an ID. | 6 |
setInterval(obj, "method", ms, arg1, …) | Calls a method of an object, with this set to the object. | 6 |
clearInterval(id) | Stops it. | 6 |
getTimer() | Milliseconds since the movie started, for timing by hand. | 4 |
Flash 5 has no timers; repeated work runs in onClipEvent(enterFrame) or
in a loop of frames that go back to each other.