0

I want to store the currency of my Incremental Game buy clicking on a button.

This is the code:

$('#saveGame').click(function() {
    localStorage.setItem('evoAmountKey','evoAmount');
});

and to load the save i used:

$('#loadGame').click(function() {
    localStorage.getItem('evoAmountKey','evoAmount');
});

but it doesn't work. And how do i save something if the var looks like this?:

var hunter = {
    Amount: 0,
    Cost: 75,
    Increment: 5
};

can i simply type:

$('#load/saveGame').click(function() {
    localStorage.getItem('hunterKey','hunter.Amount','hunter.Cost', 'hunter.Increment');
});

Can you give me a example please :)

Subodh Ghulaxe
  • 18,333
  • 14
  • 83
  • 102
Nexitis
  • 49
  • 8

1 Answers1

1

Try something like this;

var hunter = {
    Amount: 0,
    Cost: 75,
    Increment: 5
};

$("#saveGame").click(
    function() {
       // must stringify the object before save
       localStorage.setItem('hunter', JSON.stringify(hunter));
    }
);

$("#loadGame").click(
    function() {
       var savedHunter = JSON.parse(localStorage.getItem('hunter'));
       alert(savedHunter.Cost);
    }
);
Luciano
  • 234
  • 2
  • 7