localStorage命令停止程序

时间:2016-06-24 18:32:01

标签: javascript

每当我使用保存按钮和加载按钮(它们工作)但停止游戏。点击加载后,游戏冻结,按钮和动画工作,但游戏冻结,我无法获得xp或黄金。

我的代码:

function save(){
  localStorage["gold"] = gold;
  localStorage["goldmax"] = goldmax;
  localStorage["food"] = food;
  localStorage["foodmax"] = foodmax;
  localStorage["wood"] = wood;
  localStorage["woodmax"] = woodmax;
  localStorage["stone"] = stone;
  localStorage["stonemax"] = stonemax;
  localStorage["iron"] = iron;
  localStorage["ironmax"] = ironmax;
  localStorage["level"] = level;
  localStorage["xp"] = xp;
  localStorage["xpN"] = xpN;
  localStorage["xpgained"] = xpgained;
  localStorage["questpoint"] = questpoint;
  localStorage["questpointspeed"] = questpointspeed;
  localStorage["buildQueue"] = buildQueue;
  localStorage["buildSpeed"] = buildSpeed;
  localStorage["heroxp"] = heroxp;
  localStorage["herolevel"] = herolevel;
  localStorage["skillpoint"] = skillpoint;
  spop("Game has been successfully saved!")
}
function load(){
  if (localStorage.getItem("gold") === null){
    spop('<strong>Save file has been corrupted!</strong>', 'error');
  }
  if (localStorage.getItem("gold") !== null){
    gold =            localStorage["gold"];
    goldmax =         localStorage["goldmax"];
    food =            localStorage["food"];
    foodmax =         localStorage["foodmax"];
    wood =            localStorage["wood"];
    woodmax =         localStorage["woodmax"];
    stone =           localStorage["stone"];
    stonemax =        localStorage["stonemax"];
    iron =            localStorage["iron"];
    ironmax =         localStorage["ironmax"];
    level =           localStorage["level"];
    xp =              localStorage["xp"];
    xpN =             localStorage["xpN"];
    xpgained =        localStorage["xpgained"];
    questpoint =      localStorage["questpoint"];
    questpointspeed = localStorage["questpointspeed"];
    buildQueue =      localStorage["buildQueue"];
    buildSpeed =      localStorage["buildSpeed"];
    heroxp =          localStorage["heroxp"];
    herolevel =       localStorage["herolevel"];
    skillpoint =      localStorage["skillpoint"];
    spop("Game has been successfully loaded!")
  }
}

1 个答案:

答案 0 :(得分:0)

localStorage将所有数据保存在字符串中。 JSFiddle

所以你必须把它转换为数字:

parseInt(localStorage["gold"]);