使用带有setInterval的javascript

使用带有setInterval的javascript,javascript,Javascript,我只是对setinterval()有点困惑。@luctowers:这是答案 class Renderer { constructor(canvas) { this.ctx = canvas.getContext("2d"); } resize() { this.ctx.canvas.width = window.innerWidth; this.ctx.canvas.height = window.innerHeight; } }

我只是对setinterval()有点困惑。

@luctowers:这是答案

 class Renderer {

    constructor(canvas) {
    this.ctx = canvas.getContext("2d");
    }

    resize() {
    this.ctx.canvas.width = window.innerWidth;
    this.ctx.canvas.height = window.innerHeight;
    }
}

class Game {
    constructor(canvas, fps) {
    this.gfx = new Renderer(canvas);
    this.fps = fps;
    }

    start() {

    this.intervalID = setInterval(()  => this.update, 1000 / this.fps)
    }

    update() {
    this.gfx.resize(); // error undefined

    //to do
    }
}
有两件事是错的:
  • 你在游戏课上打错了字,你拼错了单词 建造师
  • 在启动功能上,的范围错误。我使用箭头 函数在类内保持相同的作用域

  • 试试看

    类实例化后的结果是什么?该渲染器是否成功返回值?