EasyManua.ls Logo

MACROMEDIA FLASH MX 2004 - ACTIONSCRIPT - Page 109

MACROMEDIA FLASH MX 2004 - ACTIONSCRIPT
816 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
Deconstructing a sample script 109
There are two onClipEvent() handlers with two different events: load and enterFrame. The
actions in the
onClipEvent(load) statement execute only once, when the SWF file loads. The
actions in the
onClipEvent(enterFrame) statement execute every time the playhead enters a
frame. Even in a one-frame SWF file, the playhead still enters that frame repeatedly and the script
executes repeatedly. The following actions occur within each
onClipEvent() handler:
onClipEvent(load) Two variables, initx and inity, are defined to store the initial x and y
positions of the
bug movie clip instance. A function is defined and assigned to the onRelease
event of the Reset instance. This function is called each time the mouse button is pressed and
released on the Reset button. The function places the ladybug back in its starting position on the
Stage, resets its rotation and alpha values, and resets the
zapped variable to false.
onClipEvent(enterFrame) A conditional if statement uses the hitTest() method to check
whether the bug instance is touching the outlet instance (
_root.zapper). There are two possible
outcomes of the evaluation,
true or false:
onClipEvent (load) {
initx = _x;
inity = _y;
_root.Reset.onRelease = function() {
zapped = false;
_x = initx;
_y = inity;
_alpha = 100
_rotation = 0;
};
}
If the hitTest() method returns true, the stopDrag() method is called, the zapper variable is
set to
true, the alpha and rotation properties are changed, and the zapped instance is told to play.
If the
hitTest() method returns false, none of the code within the curly braces ({})
immediately following the
if statement runs.
There are two
on() handlers attached to the bug instance with two different events: press and
release. The actions in the on(press) statement execute when the mouse button is pressed over
the
bug instance. The actions in the on(release) statement execute when the mouse button is
released over the
bug instance. The following actions occur within each onClipEvent() handler:
on(press) A startDrag() action makes the ladybug draggable. Because the script is attached to
the
bug instance, the keyword this indicates that it is the bug instance that is draggable:
on (press) {
this.startDrag();
}
on(release)
A stopDrag() action stops the drag action:
on (release) {
stopDrag();
}
To watch the SWF file play, see ActionScript Reference Guide Help.

Table of Contents

Related product manuals