Reputation: 85
I've been trying to save this little code but with no progress. I've used the very same save/load function and it's worked, but it was with global variables. I don't understand why it's not working in this instance. I've tried changing it in various ways all known to me but without success
The problem is in this piece of code. When I delete game.money: game.money the game works, but while it's in there the game doesn't work at all. I've tried with money: money, this.money, and even deleting the var save ={}. I realize the issue is that money is not a global variable, but I lack the understanding in how it is the issue, if anybody could help me out, that would be great.
Game code I read from here 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>
Upvotes: 0
Views: 79
Reputation: 943207
Your browser will have a set of developer tools. Please learn the basics of how to use them. The JavaScript console will be reporting an error along the lines of:
Uncaught SyntaxError: Unexpected token .
You can't use a .
character in an identifier.
If you want to use it in a property name in an object literal, you have to use a string:
"game.money": game.money
If you want a normal data structure, then you need to create your objects separately:
var save = {
game: {
money: game.money
}
};
You also need to have a game
variable containing an object somewhere in scope.
Upvotes: 2