Page 1 of 1
					
				Javascript game help.
				Posted: Mon Feb 28, 2011 5:28 am
				by n04h
				Hey guys, I'm making a JS game where every time you click the button it increases by 1. Whats happening right now is the number goes up 1 but than resets back to 0 every time, can anyone help me out.
index.html:
Code: Select all
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<script src="script.js"></script>
</head>
<body>
		<center><h1>How many times are you willing to click the button?</h1>
		<p><span id="number">0</span></p>
		<br />
		<a href="" onclick="javascript:linkClicked();">Click here</a></center>
</body>
</html>
script.js
Code: Select all
//vars
var timesClicked = 0;
function linkClicked(){
	timesClicked++;
	document.getElementById("number").innerHTML = timesClicked;
	
}
Here is a picture so you get a general idea.
 
			
					
				Re: Javascript game help.
				Posted: Mon Feb 28, 2011 9:24 am
				by Xaleph
				The scope of the variable is wrong. Either you pass the current variable inside the function, or you declare an object where the variable is a field. I would suggest using the second method.
			 
			
					
				Re: Javascript game help.
				Posted: Mon Feb 28, 2011 3:02 pm
				by hallsofvallhalla
				you dont need a semi colon when calling the function from a link.
			 
			
					
				Re: Javascript game help.
				Posted: Mon Feb 28, 2011 5:02 pm
				by Xaleph
				Like i said before, the scope of that variable is wrong. All declared variables inside functions are local variables. You are trying to increment a variable outside of the function, instead, what you are doing is making a new variable every time you call that function, and you increment that one. So yeah, that one will always be 1. 
What you can do is something like this: 
var Game = {
    timesClicked : 0,
    increment : function(){ 
       game.timesClicked++;
       document.getElementById("number").innerHTML = timesClicked;
   } 
}
			 
			
					
				Re: Javascript game help.
				Posted: Mon Feb 28, 2011 8:21 pm
				by hallsofvallhalla
				actually he declared it outside of the function therefore it is a global variable. 
var timesClicked = 0;
			 
			
					
				Re: Javascript game help.
				Posted: Mon Feb 28, 2011 9:10 pm
				by Xaleph
				Yeah, but within functions, you explicitly deal with local variables unless told otherwise.
var something = 10;
function someFunction(){
   something = 30;
   alert(something); // 30
}
alert(something); // 10
Also, on a side note, global variables like that are bad. Either create an annonymous  functions, or wrap your code within (). This should hide the code from the global namespace in Javascript engines.
			 
			
					
				Re: Javascript game help.
				Posted: Tue Mar 01, 2011 4:20 am
				by Jackolantern
				A 
closure could also be of use here, since it could hold the variable's value between function calls. They seem a bit advanced for a JS beginner at first, but I really like the explanation in the tutorial I linked.
 
			
					
				Re: Javascript game help.
				Posted: Tue Mar 01, 2011 5:52 pm
				by hallsofvallhalla
				ah sorry I confused myself on what you were referring to.  Just pass the global through the function for a easy fix.