Jump to content

Troubles with HighScore


Get_Bentley
 Share

Recommended Posts

Hello,

 

I am working on a couple games that need as highscore, I read the other threads and attempted those examples but they did not work for me. I appreciate if anyone has any kind of insight for me, I know this is a simple thing to do but its something I have not done before.

 

//Globals

this.score = 0;
        this.highScore = 0;
        this.highScoreText = this.game.add.text(150, 150, 'HIGHSCORE: ', {fontSize: '32px', fill: 'white'});
        this.scoreText = this.game.add.text(16, 325, 'Score: 0', {fontSize: '32px', fill: 'white'});
 
//in update
 
if(this.lives === 0)
{
   if (this.score > localStorage.getItem("highscore")) {
                localStorage.setItem("highscore", this.score);
 
            }
}
        this.highScoreText.content = 'HIGHSCORE: ' + localStorage.getItem("highscore");

 

 

//in game over function

 

gameOver: function(){
                       
        this.highScoreText.text = 'HIGHSCORE: ' + this.score;
 
        
    },
 
Basically while playing if lives are equal to zero it will push the current score to the HIGHSCORE text like it is supposed to but it doesnt seem to be saving the highscore, at first I though maybe it was because I was using Brackets live preview so it was playing the game directly from the file on the browser...... but even after hosting the game on a server it does not want to save the highschool to localstorage when the page is refreshed.
 
Could it possibly be that refreshing clears localstorage? Would I have to add a gameover state and a play again option that takes you back to the Game State? 
 
Any help is greatly appreciated. Thank you all and happy New Year!
Link to comment
Share on other sites

Are you trying to read "highscore" or "hischool" (from your comment, but I assume typo)?

 

It shouldn't have any problems saving the value, have you tried to print the this.score value to the console before saving it?

 

Also you can run the localstorage.getItem("hiscore") from the console (debug tools) to see what it returns.

Link to comment
Share on other sites

Refreshing shouldn't clear localStorage. You could try to move the reading/saving to localStorage for gameOver state when lives equal zero, and do everything there. Comment from MichaelD should help you debug your example and fix the problem.

If you'd still have issues you can check out my Enclave Phaser Template which contains the whole mechanism of saving highscore and then reading it.

Link to comment
Share on other sites

Refreshing will not clear local storage.

 

When you say "it doesnt seem to save highscore", have you checked the value in local storage as MichaelD suggested?

 

Underneath your `//Globals` comment (which I'm assuming is your initialisation code) you set `this.highScore` to 0, I'm unclear on where all the brackets are in your example code but can you confirm that this line:

this.highScoreText.content = 'HIGHSCORE: ' + localStorage.getItem("highscore");

gets called every frame. If it does then your `this.highscore` is completely redundant as you are just using localStorage all the time to reference values. This wont solve your problem as from the code you have posted things should work if that line is called every frame.

 

Some FYI's though:

 

localStorage saves only strings, both keys and values must be strings, in your case, you probably dont care much as JS will do comparisons tests strings vs numbers (i.e. 12 > "10" is truthy) but it is something to be aware of.

 

Brackets live preview starts a server for you, 2 infact and it is not like serving from file.

Link to comment
Share on other sites

Hey all,

 

Thank you very much for all the suggestions and help I really appreciate it. It ended up being something ridiculously simple that I was missing all I did was change

 

        this.highScoreText = this.game.add.text(150, 150, 'HIGHSCORE: ', {fontSize: '32px', fill: 'white'});
 
to
 
        this.highScoreText = this.game.add.text(150, 150, 'HIGHSCORE: ' + localStorage.getItem("highscore"), {fontSize: '32px', fill: 'white'});
 
The issue was it was saving the score into localStorage no problem but when the page was refreshed it was not displaying the 'highscore' that was saved.
 
Once again thank you all for all of you're help in this simple manner. I had just never used localStorage before :P.
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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