保存JSON不使用代码

时间:2015-01-17 08:35:48

标签: javascript json

我一直试图保存这些小代码,但没有任何进展。 我已经使用了相同的保存/加载功能并且它有效,但它是全局变量。我不明白为什么它不在这种情况下工作。 我尝试过以各种方式改变它,但我没有成功

问题在于这段代码。当我删除 game.money:game.money 时,游戏可以正常运行,但是当它在那里游戏根本不起作用时。我已经尝试过用钱:money,this.money,甚至删除var save = {} 。 我意识到问题在于钱不是一个全局变量,但我缺乏对问题的理解,如果有人可以帮助我,那将是很好的。

我从这里阅读的游戏代码Tutorial

   function save() {
  var save = {
    game.money: game.money,
  }
  localStorage.setItem("save", JSON.stringify(save));
};



//The timer to run code every second
var Timer = window.setInterval(function() {
  Tick()
}, 1000);
var buildings = [];

//The object declaration for game saves
function GameSave() {
  this.money = 1000;
  this.buildings = [];
  for (var i = 0; i < buildings.length; i++) {
    this.buildings[i] = 0;
  }
}

//The object declaration for buildings
function Building() {
  this.Name = "Lemonade Stand";
  this.Cost = 10;
  this.PerSec = 1;
}

//The function to initialise all buildings
function InitBuildings() {
  LoadBuilding("Lemonade Stand", 10, 1);
}

//The function to automatically load a building into the buildings array
function LoadBuilding(name, cost, persec) {
  var cur = buildings.length;

  buildings[cur] = new Building();
  buildings[cur].Name = name;
  buildings[cur].Cost = cost;
  buildings[cur].PerSec = persec;
}

//The function used to gather money
function GatherMoney() {
  game.money++; //++ tells javascript to add 1 to the variable

  //Display the player's current money
  document.getElementById("money").innerHTML = game.money;
}

//The function that gets run every second
function Tick() {
  for (var i = 0; i < buildings.length; i++) {
    game.money += game.buildings[i] * buildings[i].PerSec;
  }
  document.getElementById("money").innerHTML = game.money;
}



//The function to buy a lemonade stand
function Build(id) {
  if (game.money >= buildings[id].Cost) { //Check if the player has enough money, then subtract it and add a new building if they do
    game.money -= buildings[id].Cost;
    game.buildings[id] = game.buildings[id] + 1;
    document.getElementById("money").innerHTML = game.money;
    document.getElementById("Building1Qty").innerHTML = game.buildings[0];
  }
}

//Run this code once the page has loaded fully
window.onload = function() {
  InitBuildings();
  window.game = new GameSave();
};





//save and load function below

function save() {
  var save = {
    game.money: game.money,
  }
  localStorage.setItem("save", JSON.stringify(save));
};

function load() {
  var GameTwo = JSON.parse(localStorage.getItem("save"));
  if (typeof GameTwo.game.money !== "undefined") game.money = GameTwo.game.money;
  document.getElementById("money").innerHTML = game.money;
};


//AutoSave 
var saveVar;

function autoSaveFunc() {
  saveVar = setInterval(save, 2000); //Auto save every 25seconds 
}
autoSaveFunc();
//AutoLoad
var loadVar;

function autoLoadFunc() {
  loadVar = setTimeout(load, 2500); // Load once on start up, 1 second after start up.
}
autoLoadFunc();
&#13;
<!--Pleae refer to Lesson 9.txt for a full description on this lesson -->

<html>

<head>
  <title>Basic Incremental Game</title>
  <link rel="stylesheet" type="text/css" href="css/Incremental.css">
  <script src="js/Incremental.js"></script>
</head>

<body>
  <div class="container" onclick="GatherMoney();">
    <h1 style="cursor:default"> Money: <span id="money">0</span> </h1>

    <div class="Lemon" onclick="Build(0);">Lemon: <span id="Building1Cost">10</span>
      </br>PerSec: <span id="Building1PerSec">1</span>
      </br>Quantity: <span id="Building1Qty">0</span> 
    </div>




  </div>
</body>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

您的浏览器将拥有一组开发人员工具。请了解如何使用它们的基础知识。 JavaScript控制台将报告错误:

Uncaught SyntaxError: Unexpected token .

您不能在标识符中使用.字符。

如果要在对象文字中的属性名称中使用它,则必须使用字符串:

"game.money": game.money

如果您想要一个普通的数据结构,那么您需要单独创建对象:

var save = {
  game: {
      money: game.money
  }
};

您还需要在范围内的某个对象中包含一个game变量。