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.

IBPX
  • 681
  • 1
  • 6
  • 11

3 Answers3

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;
    }
}
5Mixer
  • 541
  • 4
  • 10
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

Community
  • 1
  • 1
Frank Tudor
  • 4,226
  • 2
  • 23
  • 43
  • 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()` – IBPX Mar 29 '14 at 02:33
  • 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? – IBPX Mar 29 '14 at 02:51
  • How long do you need to delay? Are you waiting for a processes or are you delaying for user input? – Frank Tudor Mar 29 '14 at 02:57
  • I'm delaying for timing (i.e. trying to make games in Haxe) – IBPX Mar 29 '14 at 03:06
  • 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: http://www.actionscript.org/forums/showthread.php3?t=257173 ...In other words because the event driven nature of Flash/Actionscript/Games there is no good solution. – Frank Tudor Mar 29 '14 at 03:11
  • Okay, so [here's](http://pastebin.com/GVx7YzLe) 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? – IBPX Mar 29 '14 at 03:26
  • 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. – IBPX Mar 30 '14 at 00:31
  • However, the script still doesn't work. _[Here's](http://pastebin.com/WyiCCPsJ)_ the updated version. – IBPX Mar 30 '14 at 00:34
  • Okay, now I'm using the `haxe.Timer.delay()` with an indirect function. I would upvote your answer, but I only have 1 rep. `:)` – IBPX Apr 13 '14 at 01:35
  • 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. – Frank Tudor Apr 13 '14 at 04:46
  • Yea, thanks for the upvote and it did let me approve your answer. – IBPX Apr 26 '14 at 02:20
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. */}
    }
}
Dewi Morgan
  • 1,143
  • 20
  • 31