Emmett Moulton
Emmett Moulton

Reputation: 1

Why isn't localStorage saving my variables?

My game is an idle one where you click protons, neutrons, and electrons and when you have enough of some, you can build hydrogen and so forth. I kind of got my local variables to work but now I am having issues with the buying stage.

Basically hydrogen costs 1 proton and one electron, when you click on the button, it runs the function SetHydrogen(), when it does that, it is supposed to run based off of the variable HydrogenCost. I am not sure if any of this is feasible.

var protons = Number(localStorage.setItem("ProtonS", Pcount));
var neutrons = Number(localStorage.NeutronS);
var electrons = Number(localStorage.ElectronS);
var hydrogens = Number(localStorage.HydrogenS);

function SaveVariables(){
  if (localStorage.getItem("ProtonS")){
    localStorage.setItem("ProtonS", Pcount);
    protons = Number(localStorage.ProtonS);
  } else {
    localStorage.ProtonS = Number(localStorage.ProtonS);
  }
  if (localStorage.NeutronS){
    localStorage.NeutronS = neutrons;
    neutrons = Number(localStorage.NeutronS);
  } else {
    neutrons = Number(localStorage.NeutronS);
  }
  if (localStorage.ElectronS){
    localStorage.ElectronS = electrons;
    electrons = Number(localStorage.ElectronS);
  } else {
    electrons = Number(localStorage.ElectronS);
  }
  if (localStorage.HydrogenS){
    localStorage.HydrogenS = document.getElementByID("HydrogenTotal").innerHTML;
    hydrogens = Number(localStorage.HydrogenS);
  } else {
    hydrogens = 0;
  }
}

function LoadVariables(){
  buying = 0;
  CanUBuy = false;
  protons = Number(localStorage.ProtonS);
  neutrons = Number(localStorage.NeutronS);
  electrons = Number(localStorage.ElectronS);
  hydrogens = Number(localStorage.HydrogenS);
}

function update(){
    protonTap.onmousedown = function() {protons = protons + 1};
    neutronTap.onmousedown = function() {neutrons = neutrons + 1};
    electronTap.onmousedown = function() {electrons = electrons + 1};

};
function draw(){
    ProtonsTotal.value = protons.toFixed(0);
    NeutronsTotal.value = neutrons.toFixed(0);
    ElectronsTotal.value = electrons.toFixed(0);
    console.log(hydrogens);
    console.log(CanUBuy);
    console.log(Pcount);
};
var mainloop = function() {update(), draw(), SaveVariables()};
var buying = 0;

function SetHydrogen(){
  buying = 1;
  HydrogenCost.buy;
  if (CanUBuy = true){
    HydrogenTotal.value ++;
    buying = 0;
    CanUBuy = false;
  } else {
    buying = 0;
  }
}


function reset(){
  CanUBuy = false;
  protons = 0;
  neutrons = 0;
  electrons = 0;
  hydrogens = 0;
  buying = 0;
}

setInterval(mainloop, 16);

var CanUBuy = false;
var HydrogenCost = new buy(1,0,1);


function buy(ProtonCost, NeutronCost, ElectronCost){
  if (buying = 1){
    this.pCost = ProtonCost;
    this.nCost = NeutronCost;
    this.eCost = ElectronCost;

    if (protons >= ProtonCost && neutrons >= NeutronCost && electrons >= ElectronCost) {
      CanUBuy = true;
      protons = protons - this.pCost;
      neutrons = neutrons - this.nCost;
      electrons = electrons - this.eCost;
    } else{
      CanUBuy = false;
      alert("You don't have enough money");
    }
  } else if (buying = 0) {
    buying = 0;
  }
}

if(!localStorage.getItem('start')){
    localStorage.setItem('start', Date.now());
 }
  var start = parseInt(localStorage.getItem('start'));
  setInterval(function(){
     ffs.value = ~~((Date.now() - start)/1e3);

  }, 1e3);

Upvotes: 0

Views: 80

Answers (1)

Jonas Wilms
Jonas Wilms

Reputation: 138235

At first, i think you should rethink your overall structure. You could shorten your code and make it reusable through OOP:

function stored(name,startvalue){
  this.name=name;
  this.value=+localStorage.getItem(name) || startvalue || 0;
}
stored.prototype={
    change:function(by){
      this.value+=by;
      localStorage.setItem(this.name,this.value);
    },
    set:function(to){
      this.value=to;
      localStorage.setItem(this.name,this.value);
    },
   valueOf:function(){ return this.value;},
};

So you can do:

var neutrons=new stored("neutrons");
alert(+neutrons);//0, 5 on reload
neutrons.set(5);
alert(+neutrons);//5

Note the + to convert the stored object to its value.

A hydrogen function could look like this:

var protons=new stored("protons",10);
var electrons=new stored("electrons",10);
var hydrogens=new stored("hydrogens");
hydrogens.buy=function(){
 if(+protons && +neutrons){
   protons.change(-1);
   neutrons.change(-1);
   this.change(1);
 }else{
   alert("impossible. Sorry :(");
 }
}

hydrogens.buy();//test

http://jsbin.com/pozinotida/1/edit?js

Upvotes: 2

Related Questions