8.8 An Alternative to Timeline Loops: setInterval( )

ActionScript for Flash MX: The Definitive Guide, 2nd Edition
By Colin Moock
Chapter 8.  Loop Statements

8.8 An Alternative to Timeline Loops:setInterval( )

As of Flash Player 6, we can use setInterval( ) to execute a function every n milliseconds. 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 ActionScript Language Reference.

     



    ActionScript for Flash MX. The Definitive Guide
    ActionScript for Flash MX: The Definitive Guide, Second Edition
    ISBN: 059600396X
    EAN: 2147483647
    Year: 2002
    Pages: 780
    Authors: Colin Moock

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net