Deconstructing a sample script

In the sample SWF file zapper.swf, when a user drags the bug to the electrical outlet, the bug falls and the outlet shakes. The main Timeline has only one frame and contains three objects: the ladybug, the outlet, and a reset button. Each of these objects is a movie clip instance.

There is one script in the SWF file; it's attached to the bug instance, as shown in the following Actions panel:

The Actions panel with the script attached to the bug instance

The bug's instance name is bug, and the outlet's instance name is zapper. In the script, the bug is referred to as this because the script is attached to the bug and the reserved word this refers to the object that contains it.

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();
}