0

我的代码有问题。为什么这个动画仍然加速。[在此链接中是代码][1]

[1]:http: enter code here //jsfiddle.net/74j0u5zf/4/

4

1 回答 1

1

因为您的多个if语句可以执行每个循环。如果 x == 500,它也 > 0。

您的gameLoop()功能可以大大简化。

function gameLoop() {

    renderer.render(stage);
    cat.x = cat.x + moveX;

    if (cat.x <= 0 || cat.x >= 500) {
        moveX = -moveX;
    }

    requestAnimationFrame(gameLoop);
}

http://jsfiddle.net/74j0u5zf/5/

于 2017-03-15T16:39:07.080 回答