O'Reilly logo

Essential ActionScript 3.0 by Colin Moock

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Animating with the ENTER_FRAME Event

The Flash runtime dispatches the Event.ENTER_FRAME event every time it performs a scheduled screen-update check (as described in Chapter 23). Any function that registers to receive Event.ENTER_FRAME notifications is executed repeatedly, at a frequency determined by the current Flash runtime frame rate. Visual changes made by any Event.ENTER_FRAME listener function are rendered after it exits.

A function can register to receive Event.ENTER_FRAME notifications from any DisplayObject instance, whether or not that instance is currently on the display list. As an example, let's use the Event.ENTER_FRAME event to implement the animation discussed in the previous section, in which a TextField moves across the screen horizontally to x-coordinate 300. We start by creating a class, TextAnimation, which creates a TextField object and then adds it to the display list.

public class TextAnimation extends Sprite {
  private var t:TextField;

  public function TextAnimation () {
    // Create a TextField
    t = new TextField();
    t.text          = "Hello";
    t.autoSize      = TextFieldAutoSize.LEFT;
    addChild(t);
  }
}

Next, we create an Event.ENTER_FRAME listener function, moveTextRight( ), which moves the TextField t to the right by 10 pixels. Invoking moveTextRight( ) repeatedly will produce the animation effect. Notice that because moveTextRight( ) is an Event.ENTER_FRAME listener function, it defines a single, required, parameter whose datatype is Event.

public function moveTextRight (e:Event):void ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required