0

I have problem with my code. Why this animation still accelerates. [in this link is code ][1]

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

1 Answers1

1

Because more than one of your if statements can execute each loop. If x == 500, it is also > 0.

Your gameLoop() function can be greatly simplified.

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/

Paul LeBeau
  • 97,474
  • 9
  • 154
  • 181