Whats wrong with this code?

Started by
16 comments, last by DangerDoom 11 years, 3 months ago

I don't know what you want or expect to happen and what you mean by "doing nothing", but your functions are empty so nothing should happen.

It also looks like you're setting the playGame variable in the wrong place. In the button callback, you call GameLoop, which then executes and nothing happens since playGame is false, then you set playGame to true and the callback is finishes. Do you, perhaps, want to set it before calling GameLoop? The second time, however, playGame is true and it should proceed to set the timer callbacks. But even if the timer callbacks start, the functions are empty and nothing will happen.

Advertisement
I don't know what you want or expect to happen and what you mean by "doing nothing", but your functions are empty so nothing should happen.

It also looks like you're setting the playGame variable in the wrong place. In the button callback, you call GameLoop, which then executes and nothing happens since playGame is false, then you set playGame to true and the callback is finishes. Do you, perhaps, want to set it before calling GameLoop? The second time, however, playGame is true and it should proceed to set the timer callbacks. But even if the timer callbacks start, the functions are empty and nothing will happen.

The functions are empty because I cut all of the code out just for the sake of this forum post since its 200 lines long, the notation describes what the code I out out does, e.g. "//code that draws the player to the screen and updates his position" is actually like 50 lines of code that i took out JUST for this forum post. I tried setting the playGame variable to true before calling the GameLoop function, and it just flashes the code that is executed from the functions. It is as if the playGame variable is only true whilst I am clicking on a button, and as soon as I stop clicking on it the boolean is immediately set to false it appears.

But you set playgame to true AFTER calling GameLoop!?

I changed this, now the code that is executed just flashes as if as soon as the boolean is set to true, it goes back to false almost immediately.

setTimeout(GameLoop, animate, collect, 33);

You should pass 2 parameters to setTimeout: The first being the function that will execute, the second being the delay before the function is executed.

arcadeButton.click(function(){
GameLoop();
playGame = true;
});

"playGame = true" should be above "GameLoop()"

Based on your code, you want the game loop to look a little more like this:


function GameLoop()
{
   if(playGame)
   {
      collect();
      animate();

      setTimeout(GameLoop, 33);
   }
};	

Run your game logic, animate, then use setTimeout to simulate a loop.

setTimeout(GameLoop, animate, collect, 33);

You should pass 2 parameters to setTimeout: The first being the function that will execute, the second being the delay before the function is executed.

arcadeButton.click(function(){
GameLoop();
playGame = true;
});

"playGame = true" should be above "GameLoop()"

Based on your code, you want the game loop to look a little more like this:


function GameLoop()
{
   if(playGame)
   {
      collect();
      animate();

      setTimeout(GameLoop, 33);
   }
};	

Run your game logic, animate, then use setTimeout to simulate a loop.

Sorry, it might be that I don't understand what you are saying, but I added the setTimeout in the button callback, and removed the other setTimeout in the GameLoop function and I have the same problem. I could be that I misunderstood what you said.

There are only two changes.

this...


arcadeButton.click(function(){
   playGame = true;
   GameLoop();
});

and this...


function GameLoop()
{
   if(playGame)
   {
      collect();
      animate();
 
      setTimeout(GameLoop, 33);
   }
};

What browser are you using to test? You'll want to learn to use the debugger. These are things you could have found, and fixed, just by using the debugger.

There are only two changes.

this...


arcadeButton.click(function(){
   playGame = true;
   GameLoop();
});

and this...


function GameLoop()
{
   if(playGame)
   {
      collect();
      animate();
 
      setTimeout(GameLoop, 33);
   }
};

What browser are you using to test? You'll want to learn to use the debugger. These are things you could have found, and fixed, just by using the debugger.

Thanks, although the issue persists. I think it may be something to do with the playGame variable, I add a console.log(playGame) after the button function and it only ever prints false.

[quote name='JackBid' timestamp='1358790941' post='5023982']

I add a console.log(playGame) after the button function and it only ever prints false.

[/quote]

I understand that you are learning all this stuff still, but challenge yourself not to use console.log

Use the debug tools in the browser. There are lots of videos on youtube on this.

This topic is closed to new replies.

Advertisement