• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
JackBid

Whats wrong with this code?

17 posts in this topic

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?

0

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.

1

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.

0

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

 

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

0

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.

1

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);
});
 
1

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.

0

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.

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


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

0

Share this post


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

0

Share this post


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

1

Share this post


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

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


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

0

Share this post


Link to post
Share on other sites

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

0

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