javascriptsvgpixi.jsgrafika

why the animation is accelerating


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/


Solution

  • 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/