Jump to content
  • Advertisement
Sign in to follow this  
JackBid

Whats wrong with this code?

This topic is 2072 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement

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

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

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

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

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
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!