Using ENTER_FRAME in Flash CS4 (AS3)

The ENTER_FRAME event is useful for making things move smoothly via ActionScript -- without using any tweens at all. How it works: Something occurs each time a new frame is entered. This is the same as saying "at the frame rate of the movie." So if the movie is 30 fps, then the thing (the event) will happen 30 times each second.

You need to upgrade your Flash player (v.9 or later) to view this example.

The movie above is star_2.fla (76 KB); it demonstrates a use of ENTER_FRAME on two buttons. Hold down either button to see something that ENTER_FRAME is good at doing.

Below, a square is moved 10 pixels to the right or left 30 times per second. There are no tweens on the Timeline.

You need to upgrade your Flash player (v.9 or later) to view this example.

The movie above is example1.fla (72 KB).

clip_mc.addEventListener(Event.ENTER_FRAME, moveClip);

function moveClip(e:Event){
if (clip_mc.x < 540) {
clip_mc.x += 10; // move the object 10 pixels to the right
} else {
clip_mc.x -= 10; // move the object 10 pixels to the left
}
}

By adding an EventListener on the movie clip instance (clip_mc), using ENTER_FRAME causes the function (moveClip) to execute for every frame in which the movie clip is present. In other words, it creates a continuous action.

As you can see above, this script does not quite do the job. In the following example, more script is added to solve the problem. Download the FLA to view the script for the example below.

You need to upgrade your Flash player (v.9 or later) to view this example.

The movie above is example2.fla (76 KB).

You need to upgrade your Flash player (v.9 or later) to view this example.

The movie above is example3.fla (96 KB).

What's cool about the third example (above) is that the buttons remove (Stop) and add (Start) the EventListener that uses the ENTER_FRAME event. As in the two previous examples, there is only one frame in this movie. There are no tweens.

Another example (below) shows how ENTER_FRAME can be used to make something happen while you are holding a button down. So long as the button stays down, the ENTER_FRAME event is used to repeat the same function again and again. Try it and see.

You need to upgrade your Flash player (v.9 or later) to view this example.

The movie above is hotter.fla (224 KB).

The ActionScript for each of these examples is on frame 1 in the FLA.

DOWNLOAD You'll find all of the FLA files in this folder.

Education use: This package was created as an example for my journalism students. It is not intended to be used commercially.

Use and re-use: Use of Enter Frame in Flash CS4 (AS3) by Mindy McAdams is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License.
Creative Commons License

Updated 7 March 2011