Event Listeners and Callback Functions

We deliberately left two features of the first Lab (Bouncing Ball) somewhat mysterious, because they require detailed explanation. Now we'll clear things up a bit, introducing two programming tools on which we'll depend pretty heavily in future work.

These are the Event Listener and the callback function. Among other things, these constructs allow us to tie operation of an ActionScript class to system and interface events, such as mouse movements, mouse button clicks, keyboard action, and in our first Lab, the all-important ENTER_FRAME.

The ENTER_FRAME event

Before we get to the code examples, let's explain what ENTER_FRAME means. It is a system event, meaning an action the Flash Player takes automatically whenever a movie is in operation. An ENTER_FRAME event is generated whenever the virtual playback head of the Flash Player enters a frame defined on a timeline. If our main movie had, say, 30 frames, this would be easy enough to visualize, especially since we can manipulate a playback head icon (that pink rectangle at the top of the editing interface), to simulate the action of the virtual playback head in the Player.

In most cases, though, the movies we'll build will have only a single frame. Logically, you might assume that there can therefore be only one ENTER_FRAME event; but you'd be wrong. In fact, all Flash movies loop by default, meaning the Player automatically returns to the first frame after playing the last frame. If there is only one frame, the Player constantly loops in and out of this frame. Most important, it generates an ENTER_FRAME event on each iteration.

The speed of this looping behavior is determined by the frame rate set for the movie, which is 12 frames per second by default, and may be reset to any value you like through the Properties panel in the Flash interface (or through ActionScript commands, if you prefer the hard way).

Event Listeners

An Event Listener is an object that constantly monitors the behavior of the Flash Player, looking for whatever Event it's programmed to detect. When the appropriate event is detected, the Listener routes execution to a custom method indicated within its syntax. This will make clearer sense if we refer to code you've already seen:

  addEventListener(Event.ENTER_FRAME, animate);

You'll recall that this line occurs within the Constructor method of scriptBallClass, the ActionScript class that animates our bouncing ball.

This instruction adds a Listener for the ENTER_FRAME event, and hands off execution to a custom method called animate().

Note a few things about the somewhat quirky syntax here. First, every Event must be given in dot notation, and in capital letters, as we do here for Event.ENTER_FRAME. The name of the event must be spelled correctly, and the keyword Event must be capitalized. Second -- and this is somewhat exasperating -- the custom method to which we hand off, called a callback function, is given without the parentheses that would normally accompany the invocation of a method. Which brings us to our final topic for this posting.

Callback functions

A callback function, or method, is simply a custom method indicated in an Event Listener somewhere in the current code base. Aside from its mention in an addEventListener() statement, this method looks like any other. In our example, animate looks something like this:

  public function animate():void
  {
    //various statements go here
  }

Since we can't include the trailing parentheses of the callback function in the addEventListener() statement, we cannot pass parameters to a callback function, as we might with an ordinary function invocation. There are ways around this limitation (through the use of public variables, for instance), but they take us into undiscussed territory.

For the moment, get familiar and comfortable with Event Listeners and callback functions. You'll use them in practically every project that involves animation or user interaction.



University of Baltimore Logo

Last updated: 02/12/08 14:52:20
Copyright © 2008 School of Information Arts and Technologies