Jump to content

Help With localstorage api


pheaset
 Share

Recommended Posts

hey guys

I'm currently after completing a tutorial at http://zenva.com . I have been working on my knowledge with phaser. anyways, after finishing the tutorial i decided to polish the game. I am new to the localstorage api and was looking for some help. 

here is my code for the highscore. it currenly doesnt work with localstorage and im not sure why. Any help is much appreciated

  init: function(score) {
    var score = score || 0;
    localStorage.setItem("score", this.highestScore);
this.highestScore = this.highestScore = localStorage.getItem("score");
this.highestScore = Math.max(score, this.highestScore);
if(this.highestScore === null)
{
  localStorage.setItem("score", "0")
this.highestScore = localStorage.getItem("score");
}
   },

Here is the full file. Just incase its needed

var SpaceHipster = SpaceHipster || {};

//title screen
SpaceHipster.MainMenu = function(){};

SpaceHipster.MainMenu.prototype = {
  init: function(score) {
    var score = score || 0;
    localStorage.setItem("score", this.highestScore);
this.highestScore = this.highestScore = localStorage.getItem("score");
this.highestScore = Math.max(score, this.highestScore);
if(this.highestScore === null)
{
  localStorage.setItem("score", "0")
this.highestScore = localStorage.getItem("score");
}
   },
  create: function() {
  	//show the space tile, repeated
    this.background = this.game.add.tileSprite(0, 0, this.game.width, this.game.height, 'space');

    //give it speed in x
    this.background.autoScroll(0, 10);

    //start game text
    var text = "Tap to begin";
    var style = { font: "30px Arial", fill: "#fff", align: "center" };
    var t = this.game.add.text(this.game.width/2, this.game.height/2, text, style);
    t.anchor.set(0.5);

    //highest score
    text = "Highest score: "+this.highestScore;
    style = { font: "15px Arial", fill: "#fff", align: "center" };

    var h = this.game.add.text(this.game.width/2, this.game.height/2 + 50, text, style);
    h.anchor.set(0.5);
  },
  update: function() {
    if(this.game.input.activePointer.justPressed()) {
      this.game.state.start('Game');
    }
  }
};

 

Link to comment
Share on other sites

You probably stored something not numeric in localStorage and now get bad results like NaN?

If that is the case try this:

init(score) {
  score = isNaN(score) ? 0 : score;
  this.highestScore = Number(localStorage.getItem("score"));
  this.highestScore = isNaN(this.highestScore) ? score : Math.max(score, Number(this.highestScore));
  localStorage.setItem('score', this.highestScore);
  return this.highestScore;
}

 

Link to comment
Share on other sites

18 hours ago, Xesenix said:

You probably stored something not numeric in localStorage and now get bad results like NaN?

If that is the case try this:


init(score) {
  score = isNaN(score) ? 0 : score;
  this.highestScore = Number(localStorage.getItem("score"));
  this.highestScore = isNaN(this.highestScore) ? score : Math.max(score, Number(this.highestScore));
  localStorage.setItem('score', this.highestScore);
  return this.highestScore;
}

 

 

17 hours ago, bruno_ said:

You can use your browser's developer tools (press F12) to inspect your local storage.

Thank you both for the help. 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

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