Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Action Script Sleep function [duplicate]

Does actionscript 2.0/3.0 have an equivalent of c# sleep() ?

like image 936
Kemrop Avatar asked Jun 09 '10 21:06

Kemrop


3 Answers

You can implement a sleep function like this:

function sleep(counter: int, subsequentFunction: Function, args: Array): void
{
    if (counter > 0)
        callLater(sleep, [counter - 1, subsequentFunction, args]);
    else
        callLater(subsequentFunction, args);
}

Call it with the function which should be processed after the pause.

// call trace('Hello') after 100 cycles
sleep(100, trace, ['Hello']);
// call myFunction() after 50 cycles
sleep(50, myFunction, []);

The advantage of this approach is that the UI is still responsive during the sleep.

like image 90
splash Avatar answered Oct 20 '22 01:10

splash


Not really. You could block (almost all) code execution with something like:

function sleep(ms:int):void {
    var init:int = getTimer();
    while(true) {
        if(getTimer() - init >= ms) {
            break;
        }
    }
}

trace("hello");
trace(getTimer());
sleep(5000);
trace("bye");
trace(getTimer());

But I don't see how could this be useful in flash. And, at the same time, anything like the above code is a very bad idea as the player will freeze and become unresponsive (and could also give a script timeout if you exceed the timeout limit, which is 15 by default).

If you merely want to delay the execution of a piece of code, you can use a Timer object or the setTimeout function. This will be non-blocking, though, so you'll have to use some kind of flag like TandemAdam suggested. It'll be brittle, at best.

Maybe there's a better approach for your problem, but it's not clear what are you trying to accomplish in your question.

like image 26
Juan Pablo Califano Avatar answered Oct 20 '22 02:10

Juan Pablo Califano


No ActionScript/Flash Player does not have an equivalent to the c# sleep function. For one thing Flash does not use multiple Threads.

You would have to implement the functionality manually.

You could use a Boolean flag, that your code would only execute when true. Then use the Timer class, for the delay.

like image 24
Adam Harte Avatar answered Oct 20 '22 01:10

Adam Harte