I don't know what do you want exactly, but I'll try :
First :
if you want to store any objects, or arrays in localStorage, I made a whole system in javascript for doing that.
let's imagine we bave this class :
class Player {
constructor(ctx,x,y,width,height,color,...etc){
...
}
...
}
now you have this variable you need to put inside local storage :
var playersList = [
new Player(attributes),
new Player(attributes2)
]
you are not allowed to store playerList
in localStorage by a simple command like localStorage.setItem("playersList", playersList);
but you need to convert them to a text sotue localStorage understands them, with the JSON wonderful API :
//The variable that will handle the text of playerList
var playersListText = JSON.stringify(playersList)
//Horray !!! you can store playersList inside localStorage !!!
localStorage.setItem("playersList", playersListText)
and once you need to get the info you just type the opposite of JSON.stringify
and it is JSON.parse
:
var playersList = JSON.parse(localStorage.getItem("playersList"))
WAIT, you will face a huge problem here : when you use JSON.parse
, you will recieve the Player
class as an object, how ? the Player
class will be converted to a simple object like :
{
ctx: someValue,
x: someValue,
y: someValue,
width: someValue,
...etc
}
so you should here loop all the objects and turn them into Player
objects just like that :
var playersList = JSON.parse(localStorage.getItem("playersList")
playersList.forEach(function(player){
if(player.type == "player") { //Here you maybe have multiple objects so you do that to separate them
player = new Player(
player.ctx,
player.x,
player.y,
...etc
)
}
})
Second :
You have a number in localStorage and you need to have some operations in it, simple !
var number = Number(localStorage.getItem("coins"))
console.log(`your added coins : ${number + 10}`)
//Include it also in for loops !
for(let i=0 ; i<=number ; i++) {
console.log(i)
}
Hope I helped you, if you want something else, just ask me