为什么localStorage没有保存我的变量?

时间:2017-06-04 18:35:21

标签: javascript local-storage

我的游戏是空闲的,你点击质子,中子和电子,当你有足够的一些,你可以建立氢等等。我有点使用我的局部变量,但现在我遇到了购买阶段的问题。

基本上氢需要1个质子和一个电子,当你点击按钮时,它运行函数SetHydrogen(),当它这样做时,它应该基于变量HydrogenCost运行。我不确定这是否可行。

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);

1 个答案:

答案 0 :(得分:2)

起初,我认为您应该重新考虑整体结构。您可以通过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;},
};

所以你可以这样做:

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

注意+将存储的对象转换为其值。

氢功能可能如下所示:

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