我一直在努力保存这个小代码,但没有进展。我使用了非常相同的保存/加载函数,而且它是有效的,但它使用的是全局变量。我不明白为什么在这种情况下不起作用。我试过用我所知道的各种方式改变它,但没有成功。
问题就在这段代码中。当我删除game.money: game.money时,游戏工作正常,但是当它在里面的时候,游戏根本就不工作了。我尝试过使用money: money,this.money,甚至删除var save ={}。我意识到问题是钱不是一个全球性的变量,但我对这个问题是如何的缺乏理解,如果有人能帮我的话,那就太好了。
我从这里读到的游戏代码,教程
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();<!--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>
发布于 2015-01-17 08:44:22
您的浏览器将拥有一组开发人员工具。请学习如何使用它们的基本知识。JavaScript控制台将按照以下方式报告一个错误:
未登录的SyntaxError:意想不到的标记。
不能在标识符中使用.字符。
如果要在对象文本中的属性名称中使用它,则必须使用字符串:
"game.money": game.money如果您想要一个正常的数据结构,那么您需要分别创建对象:
var save = {
game: {
money: game.money
}
};您还需要有一个game变量,其中包含一个作用域中的对象。
https://stackoverflow.com/questions/27997690
复制相似问题