Requestanimationframe毫秒精度

时间:2018-03-27 08:36:32

标签: javascript timedelta game-loop

我正在进行游戏循环并且无法解决特定问题:一堆对象以递增的延迟开始并且应该移动一定距离。

预期的行为是所有物体应以均匀的对角线移动,但它们会在不均匀的组中移动。

我意识到问题在于16.667ms间隔更新,它在更新周期中“分组”对象。是否有可能实现低于17ms的精度?

已经尝试分离更新和呈现方法,并在delta while循环内运行更新 - 一切都无济于事。

这是tick函数的相关部分:

function tick() {
  if (this._stopped) return;
  let now = performance.now();

  if (now < this._lastTick + this._interval - 1) {
    this._rafId = requestAnimationFrame(this.tick);
    return;
  }

  this._rafId = requestAnimationFrame(this.tick);
  let frameTime = (now - this._lastTick);
  this._lastTick = now;
  this._delta += frameTime;
  let acc = 0;

  while (this._delta >= this._interval) {
    this._delta -= this._interval;
    //this.update(this._interval);
    acc++;
  }

  this.update(acc * this._interval);
  //this.render(time);   
  this.count++;
}

这是codepen

非常感谢任何意见。

0 个答案:

没有答案
相关问题