Jump to content

  • Log In with Google      Sign In   
  • Create Account


Whats wrong with this code?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
17 replies to this topic

#1 JackBid   Members   -  Reputation: 453

Like
0Likes
Like

Posted 21 January 2013 - 04:38 AM

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?



Sponsor:

#2 Brother Bob   Moderators   -  Reputation: 8019

Like
1Likes
Like

Posted 21 January 2013 - 04:59 AM

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.



#3 JackBid   Members   -  Reputation: 453

Like
0Likes
Like

Posted 21 January 2013 - 05:07 AM

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.



#4 oldgregg   Members   -  Reputation: 227

Like
0Likes
Like

Posted 21 January 2013 - 05:29 AM

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");

 

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



#5 rip-off   Moderators   -  Reputation: 8114

Like
1Likes
Like

Posted 21 January 2013 - 05:45 AM

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.



#6 EmployeeNumber8   Crossbones+   -  Reputation: 1103

Like
1Likes
Like

Posted 21 January 2013 - 08:29 AM

Just change this...

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

 

 

 

 

to this...

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


#7 JackBid   Members   -  Reputation: 453

Like
0Likes
Like

Posted 21 January 2013 - 09:15 AM

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.



#8 Brother Bob   Moderators   -  Reputation: 8019

Like
0Likes
Like

Posted 21 January 2013 - 09:19 AM

Explain what you want to do instead so we may actually be able to tell you what a suitable solution would be.



#9 JackBid   Members   -  Reputation: 453

Like
0Likes
Like

Posted 21 January 2013 - 09:30 AM

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.



#10 smorgasbord   Members   -  Reputation: 371

Like
0Likes
Like

Posted 21 January 2013 - 09:57 AM

But you set playgame to true AFTER calling GameLoop!?


~ Not that I really have a clue

#11 Brother Bob   Moderators   -  Reputation: 8019

Like
0Likes
Like

Posted 21 January 2013 - 09:59 AM

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.



#12 JackBid   Members   -  Reputation: 453

Like
0Likes
Like

Posted 21 January 2013 - 10:08 AM

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. 



#13 JackBid   Members   -  Reputation: 453

Like
0Likes
Like

Posted 21 January 2013 - 10:11 AM

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.



#14 EmployeeNumber8   Crossbones+   -  Reputation: 1103

Like
1Likes
Like

Posted 21 January 2013 - 10:40 AM

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.



#15 JackBid   Members   -  Reputation: 453

Like
0Likes
Like

Posted 21 January 2013 - 10:56 AM

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.



#16 EmployeeNumber8   Crossbones+   -  Reputation: 1103

Like
0Likes
Like

Posted 21 January 2013 - 11:12 AM

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.



#17 JackBid   Members   -  Reputation: 453

Like
0Likes
Like

Posted 21 January 2013 - 11:55 AM

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.



#18 EmployeeNumber8   Crossbones+   -  Reputation: 1103

Like
0Likes
Like

Posted 21 January 2013 - 01:29 PM

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

 

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.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS