Hello everyone, I try to save the score information permanently with the following code
cc.sys.localStorage.setItem(‘punctuation’, total punctuation);
console.log (“datoswwwww …” + cc.sys.localStorage.getItem(‘punctuation’));
But the information is being added in a concatenated way to the registry, I need to keep a single record with the new information updated, this is the way in which the information is being stored.
-
Simulator: JS: Cocos Creator v2.1.0
-
Simulator: JS: datoswwwww … 10
-
Simulator: JS: datoswwwww … 1010
-
Simulator: JS: datoswwwww … 101010
Even if I save the information in a variable, I delete the record and add the new information, the same thing happens
cc.sys.localStorage.removeItem(‘puntuacion’);
Can you help me to know how I can do it?
I forgot to mention it, this failure occurs only when I read the scene again or when I close the simulator and execute it again, when I completely delete the file without writing anything in it and I execute the zero project works correctly, in the record a single updated value.
i save a object
var SaveData={
playerLevel:0,
score:0,
xp:0,
level:0,
mapResults:[ ],
money:0,
lives:5,
livesTimer:null
}
SaveGame()
{
cc.sys.localStorage.setItem(‘userData’, JSON.stringify(this.saveGameRef));
}
LoadSavegame()
{
this.saveGameRef=SaveData;
this.saveGameRef = JSON.parse(cc.sys.localStorage.getItem('userData'));
if(this.saveGameRef==null)
{
this.saveGameRef=SaveData;
this.saveGameRef.Init();
}
this.bInitialized=true;
}
this works ok so I believe that total punctuation var is getting several values before saving
try to console log total punctuation before setItem
With some adjustments, it worked correctly, thank you very much
1 Like