Sign in to follow this  
JackBid

Whats wrong with this code?

Recommended Posts

JackBid    453

I am not sure whats wrong with this code, it seems that whatever is in the button function does not work.

var arcadeButton = $("#Arcade");
var ininiteButton = $("#Infinite");
var helpAndAboutButton = $("#HelpAndAbout");

var num;

var hideAllTheButtons = function() {
arcadeButton.hide();
infiniteButton.hide();
helpAndAboutButton.hide();
};
 
arcadeButton.click(hideAllTheButtons);
infiniteButton.click(hideAllTheButtons);
helpAndAboutButton.click(hideAllTheButtons);

arcadeButton.click(function(){
	num = 11;
});

console.log(num);

 

whenever I run this, the console just prints "undefined" even if I repeatedly click the button. Could it be a problem with my game logic?

Share this post


Link to post
Share on other sites
Brother Bob    10344

Please specify the environment you're using and in what context it is used. This looks like a very limited piece of code. But just looking at the code, I see no reason why the callbacks would matter. The program flow seems to be like this:

  1. Get some button handles
  2. Define an uninitialized variable num
  3. Set button callbaks.
  4. Print the value of num.

There are no callbacks happening before printing in step 4, and thus no chance of printing anything but the uninitialized value from step 2.

Share this post


Link to post
Share on other sites
JackBid    453
Please specify the environment you're using and in what context it is used. This looks like a very limited piece of code. But just looking at the code, I see no reason why the callbacks would matter. The program flow seems to be like this:
  1. Get some button handles
  2. Define an uninitialized variable num
  3. Set button callbaks.
  4. Print the value of num.

There are no callbacks happening before printing in step 4, and thus no chance of printing anything but the uninitialized value from step 2.

hmm. I tried changing it to this:

var num;

var callback = false;

var hideAllTheButtons = function() {
arcadeButton.hide();
infiniteButton.hide();
helpAndAboutButton.hide();
};
 
arcadeButton.click(hideAllTheButtons);
infiniteButton.click(hideAllTheButtons);
helpAndAboutButton.click(hideAllTheButtons);

arcadeButton.click(function(){
num = 11;
callback =true;
});

if(callback){
console.log(num);
};

 

but the console just stayed blank. The environment and context is that I am making this in javascript in an html5 canvas element. The buttons are basically start game buttons. I originally had it set up with booleans to start the game, but it was not working so I made it more simple with integers to see if I can fix the problem.

Share this post


Link to post
Share on other sites
oldgregg    231

I am not sure whats wrong with this code, it seems that whatever is in the button function does not work.

var ininiteButton = $("#Infinite");

Is this meant to be :

var infiniteButton = $("#Infinite");

 

????????????

Share this post


Link to post
Share on other sites
rip-off    10976

That code you wrote is executed once, during page load. The code in callback functions may be executed later as the user interacts with the UI. However, unless you write your log statement inside s callback, it will not be run again when the values change. If you want to continually monitor the value of a variable, something like setTimeout() might be appropriate.

Share this post


Link to post
Share on other sites
DangerDoom    1103

Just change this...

arcadeButton.click(function(){
   num = 11;
});
 
console.log(num);

 

 

 

 

to this...

arcadeButton.click(function(){
   num = 11;
   console.log(num);
});
 

Share this post


Link to post
Share on other sites
JackBid    453

Just change this...

arcadeButton.click(function(){
   num = 11;
});
 
console.log(num);

 

 

 

 

to this...

arcadeButton.click(function(){
   num = 11;
   console.log(num);
});
 

when I do this, and click on the button, it only momentarily flashes the number, I think I must have to use a setTimeout.

Share this post


Link to post
Share on other sites
JackBid    453
Explain what you want to do instead so we may actually be able to tell you what a suitable solution would be.

The buttons are basically to start playing the game, there is a gameloop function with a setTimeout.


function GameLoop(){

//code that draws the player to the screen and updates his position

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

function animate(){

//code that draw rectangular squares to the screen that must be avoided
	
collect();

};

function collect(){

//code that draws circles that can be collected by the player
						
};

 

When I click the button, my plan was to run the GameLoop function and set playGame to true;

				var hideAllTheButtons = function() {
    				arcadeButton.hide();
    				infiniteButton.hide();
    				helpAndAboutButton.hide();
				};
 
				arcadeButton.click(hideAllTheButtons);
				infiniteButton.click(hideAllTheButtons);
				helpAndAboutButton.click(hideAllTheButtons);

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

 

However when I click the button nothing happens at all. I tried to simplify it with just the num var to see if I could work out the problem. As you can see I am kinda new to programming.

Share this post


Link to post
Share on other sites
Brother Bob    10344

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.

Share this post


Link to post
Share on other sites
JackBid    453
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. 

Share this post


Link to post
Share on other sites
JackBid    453
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.

Share this post


Link to post
Share on other sites
DangerDoom    1103
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.

Share this post


Link to post
Share on other sites
JackBid    453
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.

Share this post


Link to post
Share on other sites
DangerDoom    1103

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.

Share this post


Link to post
Share on other sites
JackBid    453
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.

Share this post


Link to post
Share on other sites
DangerDoom    1103

[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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this