illegal use of break statement; javascript

前端 未结 3 1289
忘了有多久
忘了有多久 2021-02-06 21:09

When this variable becomes a certain amount i want the loop to stop, but i keep getting the error, \"Uncaught SyntaxError: Illegal break statement\".

function lo         


        
3条回答
  •  失恋的感觉
    2021-02-06 21:57

    You need to make sure requestAnimFrame stops being called once game == 1. A break statement only exits a traditional loop (e.g. while()).

    function loop() {
        if (isPlaying) {
            jet1.draw();
            drawAllEnemies();
            if (game != 1) {
                requestAnimFrame(loop);
            }
        }
    }
    

    Or alternatively you could simply skip the second if condition and change the first condition to if (isPlaying && game !== 1). You would have to make a variable called game and give it a value of 0. Add 1 to it every game.

提交回复
热议问题