
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
180
|
Chapter 8: Loop Statements
Note that our time-based movement might appear jerky if the frame rate suddenly
changes. We could smooth things out by using an elapsed-time measurement that
averages the time between a series of frames instead of just two.
An Alternative to Timeline Loops:
setInterval( )
As of Flash Player 6, we can use setInterval( ) to execute a function every n millisec-
onds. For example, the following code creates a function, moveBall( ), that moves the
ball_mc movie clip 10 pixels to the right, and then uses setInterval( ) to execute that
function every 20 milliseconds.
moveBall = function ( ) {
_root.ball_mc._x += 10;
}
ballMoverID = setInterval(moveBall, 20);
To stop our interval, we can later use:
clearInterval(ballMoverID);
Like a timeline loop or an onEnterFrame( ) loop, setInterval( ) can execute a block of
code indefinitely, and it allows for screen updates between iterations. However, the
setInterval( ) function is not tied directly to a movie’s frame rate (though it is affected
by it). For complete details, see setInterval( ) in the Language Reference.
Onward!
Well, we’ve come pretty far. The end of this chapter marks a milestone—it ends our
examination of the ActionScript statements. This means we have variables, data,
datatypes, expressions, operators, and