"I am defining it in the initGame function"
No you're not. You're defining it within the success handler of an ansynchronous Ajax function that is in your initGame()
function. The success handler will not be called until later, after initGame()
finishes and after the code that called initGame()
finishes.
Try moving the call to startGameLoop()
into the Ajax success handler so that the loop won't start until initialisation is truly finished, i.e., after the Ajax response has been received. (You don't show the implementation of init_mouse()
, but you may need to move the call to that function too.)