Jump to content

Noob needs help creating a Highscore..


Learning By Doing
 Share

Recommended Posts

Hello everyone!

 

I made myself a little game, and I want to add a Highscore, do I have to store my score in the localStorage? Could someone please give me an example on how to do it?

 

in my game a have this states (boot, preloader, mainmenu, highscore & game)

//This is an example of my Game.js filemyGame.Game = function(game) { };  myGame.Game.prototype = {    create: function () {	    this.score = 0;	    this.scoreText = this.game.add.text(10, 10, 'Score: ' + this.score, { fontSize: '32px', fill: '#ffffff' });    },    update: function () {    },collect_item: function () {this.score += 10;this.scoreText.content = 'Score: ' + this.score;},};

How could I get the score from my Game state when I die, and type it out on my Highscore state? It would be great if the player could input hes name aswell. I only want top 10 or 15 players to show on the highscore..

//This is my empty Highscore.js filemyGame.Highscore = function(game) { };  myGame.Highscore.prototype = {    preload: function() {             },    create: function () {    },    update: function () {    },};

I appreciate all help I can get.

Link to comment
Share on other sites

I actually have implemented localStorage based highscore into my game here is how I did it:

//Globals HEREvar score = 0;var highscore = 0;//Update Function HERE//Psuedo code / Real codeif(player got hit by something (in other terms, he lost the game)){  //We know before this point of losing the player actually got 'some' points   if (score > localStorage.getItem("highscore")) {                localStorage.setItem("highscore", score);            }//If the score is greater than what was stored, then I tell localStorage, "Hey set 'score' as your new stored score as the highscore now//After that I display it in the update function when the player clicks "try again" or something of the sort to reset the gamehighScoreText.content = 'HIGHSCORE: ' + localStorage.getItem("highscore");

As you play my game you notice that if you get some amount of points and then close the game and come back, your highscore will still stay the same. 

 

 

Hope this gave you some insight on how to approach the problem. 

Link to comment
Share on other sites

  • 1 year later...

I actually have implemented localStorage based highscore into my game here is how I did it:

//Globals HEREvar score = 0;var highscore = 0;//Update Function HERE//Psuedo code / Real codeif(player got hit by something (in other terms, he lost the game)){  //We know before this point of losing the player actually got 'some' points   if (score > localStorage.getItem("highscore")) {                localStorage.setItem("highscore", score);            }//If the score is greater than what was stored, then I tell localStorage, "Hey set 'score' as your new stored score as the highscore now//After that I display it in the update function when the player clicks "try again" or something of the sort to reset the gamehighScoreText.content = 'HIGHSCORE: ' + localStorage.getItem("highscore");

As you play my game you notice that if you get some amount of points and then close the game and come back, your highscore will still stay the same. 

 

 

Hope this gave you some insight on how to approach the problem. 

 

Do I need to set highscore before this line score>localStorage.getItem("highscore");

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...