gpt4 book ai didi

javascript - 保存 JSON 不适用于代码

转载 作者:行者123 更新时间:2023-12-02 16:34:52 25 4
gpt4 key购买 nike

我一直在尝试保存这段小代码,但没有任何进展。我使用了完全相同的保存/加载函数并且它有效,但它使用全局变量。我不明白为什么它在这种情况下不起作用。我尝试过以我所知道的各种方式更改它,但没有成功

问题出在这段代码上。当我删除 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();
<!--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 变量,其中包含范围内某处的对象。

关于javascript - 保存 JSON 不适用于代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27997690/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com