0

I´m about to create a simple game, but the game-loop isn´t working yet. The while-loop does not break ,if I call the end_game function and keeps printing 'a'. I realy could not figure out what´s wrong with it. Thank you guys.

var game_status = true;

var end_game = function end_game(){
                    game_status = false;
                    return game_status;
                    };

var game_loop = function game_loop(game_status, end_game){
                  while(game_status == true){
                        console.log('a');
                        end_game();
                  }
              };

function call_gameloop(){
    game_loop(game_status,end_game);
};
<div id = 'play_field' onclick='call_gameloop();'></div>
  • Possible duplicate of [Passing a global variable to a function](http://stackoverflow.com/questions/18178728/passing-a-global-variable-to-a-function) – Sebastian Simon Apr 08 '17 at 19:54

1 Answers1

1

game_status variable in game_loop() function is local to the function and is different from the globally declared variable.

end_game() function modifies global variable and therefore does not break the above loop.

To make things work, don't pass game_status parameter to game_loop() function like you didn't pass to end_game() function.

Vaibhav Nigam
  • 1,334
  • 12
  • 21