Simple High Score with Array?

javascript
html5

#1

Hi!

I'm trying to make a very simple high score system in an HTML5/Javascript game that simply stores the score value in an array if it's value is more than the previous high score and does not store it's value in the array otherwise. I can get it to either continuously count up along with the current score and then reset to zero, but that doesn't help much. Anyone have ideas for how to do this? PS I don't want local storage or anything. Just a super simple system that remembers your high score for the current session until you get a higher one.

here is the code for the arrays I'm using. astrosCaught is the current score. It increments by on hit detection via astrosCaught++.

var highscore = [0];
var scoreKeeper = [];
if 	(astrosCaught > highscore[0]) 
		 {
		 highscore.unshift(astrosCaught);
		 }
	else {scoreKeeper.unshift(highscore[0]);};

here is the code for the text

ctx.fillText("high score:" + scoreKeeper[0], 10, 64);

the project is located at http://dtc-wsuv.org/rhanrahan16/final477/
let me know if you need more information, I'm pretty new to Javascript and these forums!

-Ryan


#2

Are you familiar with local storage? If so you can set the highscore in the local storage and just check if the current score of a game round is greater than that of the score and if it is set that as the highscore the code will look something like

highscore = 0
currentRoundScore = 0

if (currentRoundScore > localStorage.getItem("highscore")) { 
    localStorage.setItem("highscore", currentRoundScoree);
    }

#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.