Learning By Doing Posted March 9, 2014 Share Posted March 9, 2014 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 More sharing options...
Gamma Posted March 9, 2014 Share Posted March 9, 2014 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. Arlefreak, Learning By Doing and shohan4556 3 Link to comment Share on other sites More sharing options...
Learning By Doing Posted March 9, 2014 Author Share Posted March 9, 2014 Thank you! It did help me to store my current Level and Exp! ;-) I will work on my highscore today, More examples how to make Highscores are welcome! Link to comment Share on other sites More sharing options...
shohan4556 Posted September 24, 2015 Share Posted September 24, 2015 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 More sharing options...
Recommended Posts