Frage

I am getting started with Haxe and OpenFl, and have some experience with Javascript and Lua.
It was going pretty well, till I got to a point where I needed a function similar to wait() in Lua, etc, which stops the script until the number of seconds you set is over.

How would I go about doing this?

EDIT: To clarify, I am building to Flash.

War es hilfreich?

Lösung

Although this is old, I wanted to add another point for reference. The OP mentioned in a comment this was for a game. One method I often use is (and could probably be put in a library):

var timerCount:Float = 0;
var maxTimerCounter:Float = 5;

function update () {
    timerCounter += elapsedTime;
    if (timerCounter > maxTimerCounter){
        onTimerComplete();
        timerCount = 0;
    }
}

Andere Tipps

In SYS you are looking for:

static function sleep( seconds : Float ) : Void Suspend the current execution for the given time (in seconds).

Example: Sys.sleep(.5);

http://haxe.org/api/sys/

Edit: User is porting to flash.

So the suggestion is to use Timer

http://haxe.org/api/haxe/timer

In Timer the suggestion is to use static function delay( f : Void -> Void, time_ms : Int ) : Timer

Someone on stack overflow has an example that looks like this: haxe.Timer.delay(callback(someFunction,"abc"), 10); located here... Pass arguments to a delayed function with Haxe

For the Flash compile target, the best you can do is use a timer, and something like this setTimeout() function. This means slicing your function into two - everything before the setTimeout(), and everything after that, which is in a separate function that the timeout can call. so somethine like, eg:

tooltipTimerId = GlobalTimer.setTimeout(
    Tooltip.TOOLTIP_DELAY_MS,
    handleTooltipAppear,
    tootipParams
);

[...]

class GlobalTimer {
    private static var timerList:Array<Timer>;

    public static function setTimeout(milliseconds:Int, func:Dynamic, args:Array<Dynamic>=null):Int {
        var timer:Timer = new Timer(milliseconds);
        var id = addTimer(timer, timerList);
        timer.run = function() {
            Reflect.callMethod(null, func, args);
            clearTimeout(id);
        }   
        return id;
    }

    private static function addTimer(timer:Timer, arr:Array<Timer>):Int {
        for (i in 0...arr.length) {
            if (null == arr[i]) {
                arr[i] = timer;
                return i;
            }
        }
        arr.push(timer);
        return arr.length -1;
    }

    public static function clearTimeout(id:Int) {
        var timers:Array<Timer> = GlobalTimer.getInstance().timerList;
        try {
            timers[id].stop();
            timers[id] = null;
        } catch(e:Error) {/* Nothing we can do if it fails, really. */}
    }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top