使用 HTML 5 保存对象
Save object using HTML 5
我无法让这个本地存储工作。这是一个使用javascript的小游戏。这是我尝试使用的保存和加载功能。有没有更好的方法可以做到这一点?要调用保存和加载函数,我正在使用按钮。
编辑:我已经编辑以包含完整的代码。
var snowballs = 0.0;
function userClick(number){
snowballs = snowballs + number;
document.getElementById("snowballs").innerHTML = prettify(snowballs);
}
function snowballClick(number){
snowballs = snowballs + (number* 0.1) ;
document.getElementById("snowballs").innerHTML = prettify(snowballs);
};
var penguins = 0.0;
var igloos = 0;
function buyPenguin(){
var penguinCost = Math.floor(10 * Math.pow(1.1,penguins)); //works out the cost of this cursor
if(snowballs >= penguinCost){ //checks that the player can afford the cursor
penguins = penguins + 1; //increases number of cursors
snowballs = snowballs - penguinCost; //removes the cookies spent
document.getElementById('penguins').innerHTML = penguins; //updates the number of cursors for the user
document.getElementById('snowballs').innerHTML = snowballs; //updates the number of cookies for the user
};
var nextCost = Math.floor(10 * Math.pow(1.1,penguins)); //works out the cost of the next cursor
document.getElementById('penguinCost').innerHTML = nextCost; //updates the cursor cost for the user
};
function save(){
var save = {
snowballs: snowballs,
penguins: penguins,
igloos: igloos,
prestige: prestige
}
localStorage.setItem("save",JSON.stringify(save));
};
function load(){
var savegame = JSON.parse(localStorage.getItem("save"));
if (typeof savegame.snowballs !== "undefined") snowballs = savegame.snowballs;
if (typeof savegame.penguins !== "undefined") penguins = savegame.penguins;
if (typeof savegame.igloos !== "undefined") igloos = savegame.igloos;
document.getElementById('penguins').innerHTML = penguins;
document.getElementById('snowballs').innerHTML = snowballs;
document.getElementById('igloos').innerHTML = igloos;
};
function prettify(input){
var output = Math.round(input * 1000000)/1000000;
return output;
}
function buyIgloos(){
var iglooCost = Math.floor(10 * Math.pow(3,igloos)); //works out the cost of this cursor
if(snowballs >= iglooCost){ //checks that the player can afford the cursor
igloos = igloos + 1; //increases number of cursors
snowballs = snowballs - iglooCost; //removes the cookies spent
document.getElementById('penguins').innerHTML = penguins; //updates the number of cursors for the user
document.getElementById('snowballs').innerHTML = snowballs; //updates the number of cookies for the user
document.getElementById('igloos').innerHTML = igloos;
};
var nextIglooCost = Math.floor(10 * Math.pow(3,igloos)); //works out the cost of the next cursor
document.getElementById('iglooCost').innerHTML = nextIglooCost; //updates the cursor cost for the user
};
function devGive(){
snowballs = snowballs + 100
document.getElementById("snowballs").innerHTML = prettify(snowballs);
}
window.setInterval(function(){
snowballClick(penguins);
userClick(igloos)
}, 1000);
<html>
<head>
<link rel="stylesheet" type="text/css" href="interface.css" />
</head>
<body>
<button onclick="userClick(1)">Click Me!</button>
<br />
Snowballs: <span id="snowballs">0</span>
<br />
<button onclick="buyPenguin()">Buy Penguin</button>
<br />
Penguins: <span id="penguins">0</span>
<br />
Penguin Cost: <span id="penguinCost">10</span>
<br />
<button onclick="save()">Save!</button>
<br />
<button onclick="load()">Load!</button>
<br />
<button onclick="buyIgloos()">Buy Igloo</button>
<br />
Igloos: <span id="igloos">0</span>
<br />
Igloo Cost: <span id="iglooCost">10</span>
<button onclick="devGive()">dev give 100</button>
<script type="text/javascript" src="main.js"></script>
</body>
</html>
我认为问题是你没有定义声望变量。
var penguins = 0.0;
var igloos = 0;
var snowballs = 0.0;
var prestige = 'prestige'
function save(){
var save = {
snowballs: snowballs,
penguins: penguins,
igloos: igloos,
prestige: prestige
}
localStorage.setItem("save",JSON.stringify(save));
};
看到这个链接,它将工作: jsfiddle
相关文章:
- 如何使用Phonegap将图像对象保存到Android中的文件中
- 如何将对象保存到谷歌UI实例中
- 将表单对象保存到具有主干.js的模型
- 如何将非常大的内存中对象保存到文件
- 将对象保存在数组中以获取高分列表
- 为什么不可能将对象保存在二维数组中
- 使用php脚本将javascript对象保存到数据库中
- 将对象保存到angularjs中的JSON文件中
- 对象保存时未调用分析回调
- KineticJS:保存到JSON:对象保存了多次
- 如何将json对象保存为文件服务器端
- 将Javascript对象保存到NoSQL数据库中,然后检索它
- 通过NodeJS将对象保存到DynamoDB实例
- 将画布对象保存为图像时出现安全错误
- 将DOM对象保存在内存中Vs将HTML字符串保存在内存中以便重复使用
- 我如何将对象保存到Node.js中的文件
- 如何将对象保存为json字符串到数据库并稍后重新加载
- 将Javascript文件对象保存到本地存储
- 在AngularJS中将对象保存到控制器中的数组中
- 字符编码- Javascript Blob对象保存为文件包含额外的字节