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.

Share on FacebookShare on Google+Share on StumbleUponTweet about this on TwitterEmail this to someone

Facebook comments:

One thought on “A Simple Pause Function in Actionscript 3”

  1. Hello Sir,
    First of all Thanks for great job.I am Flex developer since last 2 year.
    I have some doubt regarding to flex technologies.since Flex donate to appache by adove.so what do you think about this should we switch to another technologies or there is any scope of flex.

Leave a Reply