I'm trying to learn how use the localStorage
for a pacman game.
The idea is one pacman eats a pellet which deletes it from page and adds it to localStorage
so you refresh the page to pellet are still gone.
My problem is that only getting last Key Id from key list back, but what I want is an array to store all numbers and get them all back.
My code is below:
for (var j in key_list) {
keyId = key_list[j].GetUserData().val;
stage.removeChild(pacdotsE[keyId]);
world.DestroyBody(key_list[j]);
//console.log(keyId);
localStorage.setItem("key_list",keyId);
}
key_list.length = 0;
function readStorage() {
console.log(localStorage.getItem("key_list"));
keyId = localStorage.getItem("key_list");
}
The problem is that in localStorage you are only writing the latest keyId.
for (var j in key_list) {
...
localStorage.setItem("key_list",keyId);
}
What you can do is to save an array to string of the keys like so:
for (var j in key_list) {
...
keyIdArr.push(keyId)
localStorage.setItem("key_list",keyIdArr.toString());
}