Wait() in Haxe?
Asked Answered
N

3

6

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.

Nuli answered 29/3, 2014 at 2:13 Comment(0)
H
5

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;
    }
}
Hockey answered 6/8, 2015 at 9:3 Comment(0)
L
4

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

Lallage answered 29/3, 2014 at 2:19 Comment(11)
Sorry, forgot to mention I am building to Flash, and Sys only supports Neko, PHP, C++, CS, and Java, so I get a Accessing this field requires a system platform (php,neko,cpp,etc.) error when I try to build while using Sys.sleep()Nuli
I know you can do haxe.Timer.delay(a, b) to do a after b milliseconds, but the rest of the script doesn't wait for it. Is there some sort of this.stop() and this.resume() type combination I could use?Nuli
How long do you need to delay? Are you waiting for a processes or are you delaying for user input?Lallage
I'm delaying for timing (i.e. trying to make games in Haxe)Nuli
It is probably terrible to do this but have you considered a while loop? If you are doing flash with haxe then you should check out what the actionscript guys do when they run into situations like this... Example: actionscript.org/forums/showthread.php3?t=257173 ...In other words because the event driven nature of Flash/Actionscript/Games there is no good solution.Lallage
Okay, so here's my current Sleep() function, but I keep getting an error Float should be Int when I try to build. Do you see anything I'm doing wrong?Nuli
Well, I fixed my problem (a rather stupid one), I had it set to add 0.01 to elapsedTime, which is an integer, every 10 milliseconds, effectively keeping track of the time... not realizing intergers are only whole numbers, no decimals. Changing elapsedTime:Int to elapsedTime:Float fixed the error.Nuli
However, the script still doesn't work. Here's the updated version.Nuli
Okay, now I'm using the haxe.Timer.delay() with an indirect function. I would upvote your answer, but I only have 1 rep. :)Nuli
I'm glad you got a solution working...+1 to your question (so you you have more than one rep now). Come back and upvote me when you get some mileage on your SO account. You can also select the green check if it solved your problem. I don't think that has rep limitations.Lallage
Yea, thanks for the upvote and it did let me approve your answer.Nuli
G
0

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. */}
    }
}
Gogetter answered 29/3, 2014 at 2:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.