A Simple Pause Function in Actionscript 3

Just a quick function that you can use in your library of tricks to simulate pausing in Actionscript 3.

1
2
3
4
5
6
7
8
9
10
11
public static function pause(timeInSeconds:int, functionToCall:Function):void {
    var timer:Timer = new Timer(timeInSeconds * 1000);
    timer.addEventListener(TimerEvent.TIMER, callFunction, false, 0, true);
    timer.start();
    function callFunction(event:TimerEvent):void {
        timer.stop();
        timer.removeEventListener(TimerEvent.TIMER, callFunction);
        timer = null;
        functionToCall();              
    }
}

The function takes 2 arguments:

timeInSeconds – how many seconds to wait before calling the function
functionToCall – the function to call after the given time has passed

example usage (assuming that you drop this function into a class called Utilities:

1
2
3
4
5
6
7
8
9
10
11
private function gameWinTriggered():void{
 
     // player just dropped the last piece on the game board,
     // but we want to wait a second before showing the win screen
   
    Utilities.pause(1,showWinScreen);
}

private function showWinScreen):void{
     trace('You Win!');
}

That’s all there is to it. Really handy for instances like these when you want to wait before a transition as to not jar the player with instant changes.

You can leave a response, or trackback from your own site.

Facebook comments:

Leave a Reply

You must be logged in to post a comment.