我正在学习Haxe和OpenFL,并且有一些Javascript和Lua的经验。
一切进展顺利,但是我遇到了一个问题,我需要类似于Lua中
一切进展顺利,但是我遇到了一个问题,我需要类似于Lua中
wait()
的函数,该函数在你设置的秒数结束之前停止脚本运行。
我该如何解决这个问题?
编辑:为澄清起见,我正在构建Flash。
虽然这篇文章有点老了,但我想再补充一点参考意见。评论中提到这是为游戏而写的。我经常使用的一种方法是(并且可能可以放入库中):
var timerCount:Float = 0;
var maxTimerCounter:Float = 5;
function update () {
timerCounter += elapsedTime;
if (timerCounter > maxTimerCounter){
onTimerComplete();
timerCount = 0;
}
}
在SYS中,您需要寻找:
静态函数sleep(秒:浮点数):Void 暂停当前执行给定的时间(以秒为单位)。
例如:Sys.sleep(.5);
编辑:用户正在转移到Flash。
因此建议使用Timer。
http://haxe.org/api/haxe/timer
在计时器中,建议使用 静态函数delay(f:Void -> Void,time_ms:Int):计时器
stackoverflow上有一个示例,看起来像这样:haxe.Timer.delay(callback(someFunction,“abc”),10);位于此处...Pass arguments to a delayed function with Haxe
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. */}
}
}
Sys.sleep()
进行构建时,会出现“访问此字段需要系统平台(php、neko、cpp等)”的错误。 - IBPXhaxe.Timer.delay(a, b)
在b
毫秒后执行a
,但是脚本的其余部分不会等待它。有没有类似于this.stop()
和this.resume()
的组合可以使用? - IBPXSleep()
函数的代码(链接:http://pastebin.com/GVx7YzLe),但是在尝试构建时我一直收到“浮点数应该是整数”的错误提示。你看到我做错了什么吗? - IBPX