Problem with local storage

Hi guys,
i have a problem with local storage. Here is my two codes :
the fisrt one to save the data seems ok (last line):

addScore(){
this.gameScore ++;
this.scoreLabel.string = 'Score : ’ + this.gameScore.toString();
cc.sys.localStorage.setItem(“myScore”, ‘this.gameScore’);
}

but the second to retrieve the value is not… :

var myItem = localStorage.getItem(“myScore”);

Those are in two scenes different but the problem with the second code is that it don’t accept the “var”.
I’m using typeScript.

I need help please :slight_smile:

shouldn’t the line to retrieve the score be like this?

var myItem = cc.sys.localStorage.getItem(“myScore”);

Thanks! now it works… partially

updateScore(){
    this.myscore = cc.sys.localStorage.getItem('myScore');
    var myItem = cc.sys.localStorage.getItem('myScore');
    this.scoreLabel.string = 'Score : ' + myItem.toString();
}

I made those lines in a function but how can i call it now ? because if i type updateScore() beneath it does not works

You need to call it from onLoad or whenever you want to show your score on the screen.

For example you can make a score screen and put this component on it. Call updateScore() in components onEnable function. This way it’ll update label when you show the scrore screen.

Check life cycle callbacks in documents:
https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html?h=lifetime

1 Like