Javascript game help.

Need help with an engine or coding not on the list? Need help with a game or the website and forums here? Direct all questions here.
Post Reply
n04h
Posts: 151
Joined: Thu Feb 10, 2011 4:48 am

Javascript game help.

Post 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.
Image
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: Javascript game help.

Post 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.
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: Javascript game help.

Post by hallsofvallhalla »

you dont need a semi colon when calling the function from a link.
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: Javascript game help.

Post 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;
}
}
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: Javascript game help.

Post by hallsofvallhalla »

actually he declared it outside of the function therefore it is a global variable.

var timesClicked = 0;
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: Javascript game help.

Post 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.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Javascript game help.

Post 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.
The indelible lord of tl;dr
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: Javascript game help.

Post by hallsofvallhalla »

ah sorry I confused myself on what you were referring to. Just pass the global through the function for a easy fix.
Post Reply

Return to “Advanced Help and Support”