ES6 类方法不是函数

Cow*_*gun 6 html javascript ecmascript-6

我正在摆弄 Javascript“类”,并且我有一个桨,它有正确的绘制方法,但由于某种原因,我的 moveBall 函数被搞乱了。有人能指出为什么吗?我收到一条错误消息,指出 moveBall() 不是函数。

编辑:我添加了更多代码,我调用 init() 来启动这一切。

class Ball {
    constructor(x, y, r, sAngle, rAngle) {
        this.x = x;
        this.y = y;
        this.r = r;
        this.sAngle = sAngle;
        this.rAngle = rAngle;
        this.speed = null;
    }

    drawBall() {
        ctx.beginPath();
        ctx.arc(this.x, this.y, this.r, this.sAngle, this.rAngle);
        ctx.fillStyle = "#FF0000";
        ctx.fill();
    }
    moveBall() {
        this.x += this.speed;

    }

}


function init() {
    var  ball = new Ball(c.height / 2, c.width / 2, 10, 0, 2 * Math.PI);
    var paddleLeft = new Paddle(0, 0, 20, 100);
    ball.ballPhysics = 1.0;
    draw(ball, paddleLeft);
    main(ball);
}


window.main = function (ball) {
    window.requestAnimationFrame(main);
    ball.moveBall();
    window.onload = function () {
    document.addEventListener('keydown', function (event) {
        if (event.keyCode === 65) {

        }
    }, false);
}

};
Run Code Online (Sandbox Code Playgroud)

Byr*_*gin 1

确保您尝试运行的代码范围内有一个 ball 实例,并尝试var moveball = function(){this.x += this.speed; };查看它是否是编译问题,并确保您像这样访问它ball.moveball()