最も単純な形式では:
localStorage.setItem('index0', JSON.stringify(playerInventory[lootArray[0]]));
var retrievedIndex0 = localStorage.getItem('index0');
console.log('retrieved0:'+retrievedIndex0 ); //Displays correct amount
それで動いていると思っていたのですが、getItemを使う直前にsetItemをしないとnullが返ってきたり、setItemをしてF5を打ったら「index0」がnullになったり…。
if(localStorage.getItem('index0') < playerInventory[lootArray[0]]) {
console.log('bingo! ');
localStorage.setItem('index0', JSON.stringify(playerInventory[lootArray[0]])); }
この ^ は「ビンゴ!」を出力します。(firefox で) ログに記録されますが、setItem は localStorage を更新しません。ゲームをリプレイすると、getItem の前に setItem を再度設定しないと null になります...
これも正しく発火します...
var oldscore = localStorage.getItem('index0');
var newscore = playerInventory[lootArray[0]];
if(oldscore < newscore) { console.log('new high score');}
しかし、setItem は localStorage を更新しません....f5 を押してゲームをリプレイすると、null になります
if(oldscore < newscore) {
console.log('new high score'); //This fires
localStorage.setItem('index0', JSON.stringify(playerInventory[lootArray[0]])); //this doesnt
}
メインの update() ループに移動して、次のように入力すると:
console.log(localStorage.getItem('index0')); //returns null
console.log(localStorage.getItem.index0); //returns undefined
誰かが何か間違ったことを見たり、私ができる他のテストがありますか?
playerInventory[lootArray[0]] は、localstorage と比較して、より高い場合は localstorage に更新するために使用したい数値を保持しています....そのオブジェクトを使用して、画面上の div を現在の値で更新します。保存/復元したい整数.............................................
IE でテストしたところ、エラーがまったく機能しません。この投稿の上部にある基本的な最初の部分でさえ、エラーがスローされます。
SCRIPT5007: プロパティ 'getItem' の値を取得できません: オブジェクトが null または未定義です
getItem を使用する前に同じ関数でアイテムを設定できますが、その時点では明らかに null ではありませんが、その時点を超えて持続しません......