延迟1帧的代码怎么写

游戏是60帧的:director->setAnimationInterval(1.0f / 60);

runAction(DelayTime(1 / 30.f)) 还是 runAction(DelayTime(1 / 60.f))

应该用哪种写法?

game.deltaTime

this.scheduleOnce(() => {
   console.log("下一帧执行");
}, 0);

我就举个例子,假如延迟3帧、4帧、该怎么写

用时间来决定帧数是不真实的,因为每帧的时间都存在波动,cpu计算过多时间增大,就导致无法准确的用时间定义它代表了多少帧。

不过你可以自己封装一个函数,不用时间作为执行条件,用帧数来决定执行条件,通过update函数来获取当前是第几帧。

好的,大佬。

我之前写过一个类似延时函数管理,就是利用update机制获取时间增量以及记录update调用次数,来实现我想延时固定时间或帧数后执行一个函数的逻辑。

逻辑类似:

    // targetTime 可以改成targetFrame
    private funList: { targetTime: number, callback: () => void }[] = [];
    private curTime: number = 0;
    private curFrame: number = 0;

    public update(dt): void {
        this.curTime += dt;
        this.curFrame++;
        for (let i = 0; i < this.funList.length; i++) {
            let fun = this.funList[i];
            // 如果是targetFrame 需要与curFrame比较
            if (fun.targetTime > this.curTime) {
                continue;
            }
            this.funList.splice(i, 1)[0].callback();
            i--;
        }
    }

    // time可以改为frame
    public addFun(time: number, callback: () => void): void {
        // 如果是frame则需要是this.curFrame+frame
        let targetTime = this.curTime + time;
        this.funList.push({ targetTime: targetTime, callback: callback });
    }

不建议这样的写法 。
使用 setTimeout(function(){},0) ; 就是下一帧。
这里的0不是真的0毫秒,立刻执行,而是计算机能最快分给你的下一个执行时间。你可以测试试试。

1赞