requestAnimationFrame 的 RangeError

发布于 2024-12-25 11:18:31 字数 1494 浏览 2 评论 0原文

我收到以下错误:

RangeError: Maximum call stack size exceeded.

在这一行:

requestAnimFrame(Game.loop());

在这段代码中:

var Game = {
  canvas: null,
  context: null,
  targetFps: 60,

  init: function() {
    canvas = document.getElementById('main-canvas');
    context = canvas.getContext('2d');

    // Resize canvas to match content
    var content = document.getElementById('primary-content');
    canvas.width = content.offsetWidth;
    canvas.height = content.offsetHeight;

    window.requestAnimFrame = (function() {
      return window.requestAnimationFrame || // Chromium
        window.webkitRequestAnimationFrame || // WebKit
        window.mozRequestAnimationFrame || // Mozilla
        window.oRequestAnimationFrame || // Opera
        window.msRequestAnimationFrame || // IE
        null;
    })();

    this.loop();
  },

  loop: function() {
    requestAnimFrame(Game.loop());
  },

  update: function() {
    // ...
  },

  draw: function() {
    // Clear background with black
    context.fillStyle = '#000';
    context.fillRect(0, 0, canvas.width, canvas.height);
  }
};

Game.init();
<div id="primary-content">
  <canvas id="main-canvas" width="400" height="400"></canvas>
</div>

这可能是我忽略的明显事情,但任何帮助将不胜感激。谢谢!

I'm getting the following error:

RangeError: Maximum call stack size exceeded.

on this row:

requestAnimFrame(Game.loop());

in this piece code:

var Game = {
  canvas: null,
  context: null,
  targetFps: 60,

  init: function() {
    canvas = document.getElementById('main-canvas');
    context = canvas.getContext('2d');

    // Resize canvas to match content
    var content = document.getElementById('primary-content');
    canvas.width = content.offsetWidth;
    canvas.height = content.offsetHeight;

    window.requestAnimFrame = (function() {
      return window.requestAnimationFrame || // Chromium
        window.webkitRequestAnimationFrame || // WebKit
        window.mozRequestAnimationFrame || // Mozilla
        window.oRequestAnimationFrame || // Opera
        window.msRequestAnimationFrame || // IE
        null;
    })();

    this.loop();
  },

  loop: function() {
    requestAnimFrame(Game.loop());
  },

  update: function() {
    // ...
  },

  draw: function() {
    // Clear background with black
    context.fillStyle = '#000';
    context.fillRect(0, 0, canvas.width, canvas.height);
  }
};

Game.init();
<div id="primary-content">
  <canvas id="main-canvas" width="400" height="400"></canvas>
</div>

It's probably something obvious I'm overlooking, but any help would be appreciated. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一个人练习一个人 2025-01-01 11:18:31

您立即调用该函数而不是传递它:

loop: function() {
        requestAnimFrame(Game.loop());
    }

只会一遍又一遍地执行 Game.loop()

您想要的是:

loop: function() {
        requestAnimFrame(Game.loop);
    }

这会将函数作为参数传递给 requestAnimFrame,而不是调用 Game.loop() 并传递结果 (undefined)。

You are calling the function right away instead of passing it:

loop: function() {
        requestAnimFrame(Game.loop());
    }

Will just keep doing Game.loop() over and over again.

What you want is:

loop: function() {
        requestAnimFrame(Game.loop);
    }

this will pass the function as an argument to requestAnimFrame, instead of calling Game.loop() and passing the result (undefined).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文