Creating Continuous Actions with enterFrame

So far, you have learned ways to execute an action in response to events that happen when the user does something. But on many occasions, you’ll want to perform an action continuously. An if statement, for example, often needs to be performed continuously to check whether conditions in the movie have changed. And often, the command that changes the position of a movie clip needs to be performed continuously to animate it across the Stage.

The clip event enterFrame is an event that happens continuously. The clip event happens at the frame rate of the movie, so if the frame rate is set to 12 frames per second, the enterFrame event is triggered 12 times per second. Even when the enterFrame event is assigned to a movie clip whose Timeline is stopped, the event will continue to happen. This setup is an ideal way to make actions run on automatic pilot; they will run as soon as the enterFrame event handler is established and stop only when the movie clip to which the enterFrame is attached is removed.

To create continuous actions with enterFrame:

1.

Select the first frame of the main Timeline, and open the Actions panel.

2.

In the Script pane, enter _root. From the code hint pull-down menu that appears, select onEnterFrame.

or

Choose Built-in Classes > Movie > MovieClip > Events > onEnterFrame.

3.

Directly to the right of the onEnterFrame event, enter an equals sign. Select Statements > User-Defined-Functions > function (Figure 4.111).

Figure 4.111. The enterFrame clip-event handler.

4.

Insert a new line in between the curly braces of the function and choose Statements > Conditions/Loops > if.

5.

For the condition parameter (between the opening and closing parentheses of the if statement), enter this statement:

Key.getCode()==Key.SPACE

This statement is the same conditional statement that you constructed in the preceding task. In this case, however, Flash does not wait for a keyDown event before testing this condition. Here, Flash tests the condition each time a frame in the main Timeline updates. This test happens continuously, even if the Timeline is stopped.

6.

Choose an action as the consequence of this if statement (Figure 4.112).

Figure 4.112. Flash continuously monitors whether the last key pressed was the spacebar. If so, it moves the playhead to Frame 5 and stops there.

✓ Tips

Here’s a preview of the kind of dynamic updates you can do with the enterFrame clip event by combining it with movie-clip properties. Replace the if statement in the preceding task with the expression ball_mc._xscale += 1. Now add a movie-clip instance to the Stage, and name it ball_mc in the Property Inspector. The Script pane should look like this:

_root.onEnterFrame = function() {
ball_mc._xscale += 1; }

The statement ball_mc._xscale += 1 adds 1 percent to the horizontal width of your movie clip. Because the action is triggered continuously by the enterFrame event, the movie clip keeps growing.

Be careful of over-using the onEnterframe/onClipEvent(enterFrame) event handler because it can be processor-intensive. After you no longer need the event handler, it’s good practice to delete the movie clip that is associated with it. This will prevent Flash from having to execute the event needlessly.