保存不使用代码的 JSON
Save JSON not working with code
我一直在尝试保存这个小代码,但没有进展。我使用了完全相同的保存/加载函数并且它有效,但它是全局变量。我不明白为什么它在这种情况下不起作用。我尝试过以我所知道的各种方式更改它,但没有成功
问题出在这一段代码中。当我删除game.money:game.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>
您的浏览器将具有一组开发人员工具。请了解如何使用它们的基础知识。JavaScript 控制台将报告如下错误:
Uncaught SyntaxError: Unexpected token .
不能在标识符中使用.
字符。
如果要在对象文本的属性名称中使用它,则必须使用字符串:
"game.money": game.money
如果你想要一个普通的数据结构,那么你需要单独创建你的对象:
var save = {
game: {
money: game.money
}
};
您还需要有一个包含作用域中某处对象的game
变量。
相关文章:
- 如何将数组循环到JSON代码中
- 从外部json代码向amcharts图表添加数据
- 这个HTML、JavaScript和JSON代码有什么问题
- 在这种情况下,如何访问我的JSON代码
- json代码中的重定向
- 从 json 代码中删除 javascript 对象变量名称文本
- FreeCodeCamp JSON 代码不起作用
- 为什么我的 JSON 代码使用自己的输出(来自 JSON.stringify)返回“语法错误”
- 简单的 JSON 代码不起作用
- 滴灌API Json代码不起作用
- 获取 JSON 代码语法错误
- React JS未格式化JSON代码
- 如何在JavaScript文件中从PHP编写JSON代码
- 将JSON代码传输到外部文件时出现问题
- JS Json代码没有在视图源上输出数据
- 从Textarea获取JSON代码并解析它
- 如何检索数据从mssql数据库到json代码与asp.net
- 使用c#将JSON代码导入Javascript文件
- 我的JSON代码有什么问题?
- 从 JSON 代码点火器中的控制器检索数据