在Javascript中保存和加载不起作用
Saving and loading in Javascript not working
我目前正在用HTML和Javascript制作一个简单的游戏(我的第一个)。这是用于保存的代码:
function save()
{
var save = {
ducks: ducks,
ponds: ponds,
pondCost: pondCost,
ponds: ponds,
vacuums: vacuums,
vacuumCost: vacuumCost,
vacuum: vacuum,
llama: llama,
llamaCost: llamaCost,
llamas: llamas
}
localStorage.setItem("save",JSON.stringify(save));
};
这是用于加载的代码:
function load()
{
var savegame = JSON.parse(localStorage.getItem("save"));
if (typeof savegame.ducks !== "undefined") ducks = savegame.ducks;
if (typeof savegame.ponds !== "undefined") ponds = savegame.ponds;
if (typeof savegame.pond !== "undefined") pond = savegame.pond;
if (typeof savegame.pondCost !== "undefined") pondCost = savegame.pondCost;
if (typeof savegame.vacuums !== "undefined") vacuums = savegame.vacuums;
if (typeof savegame.vacuum !== "undefined") vacuum = savegame.vacuum;
if (typeof savegame.vacuumCost !== "undefined") vacuumCost = savegame.vacuumcost;
if (typeof savegame.llama !== "undefined") llama = savegame.llama;
if (typeof savegame.llamas !== "undefined") llamas = savegame.llamas;
if (typeof savegame.llamaCost !== "undefined") llamaCost = savegame.llamaCost;
document.getElementById('ponds').innerHTML = ponds;
document.getElementById('vacuums').innerHTML = vacuums;
document.getElementById('vacuumCost').innerHTML = nextCost;
document.getElementById('pondCost').innerHTML = nextCost;
};
我还添加了一个重置功能,但是一旦我添加了它并按下重置按钮,保存就不起作用了!(或加载)
function reset()
{
localStorage.removeItem("save")
}
所有这些按钮都附加到我的HTML中的一个按钮上。
在浏览器中玩游戏:https://dl.dropboxusercontent.com/u/104187515/TIGWNN/index.html
好吧,在没有完全调试您的代码的情况下,我在点击保存时出现错误。第 17 行的保存函数正在执行:
vacuum: vacuum,
您没有名为 vacuum 的全局变量,但您有一个名为 vacuums
的全局变量。该错误正在停止任何进一步的处理。
相关文章:
- 弹出加载不起作用
- GULP中的Ctrl+S之后,实时重新加载不起作用
- 影子盒加载不起作用
- 余烬 CLI 实时重新加载不起作用
- 在Javascript中保存和加载不起作用
- 路由到新页面时加载不起作用
- IE11 脚本加载不起作用(有时)
- 砌体图像加载不起作用
- Jquery 移动加载不起作用
- 在javascript/jquery中加载不起作用
- jquery瀑布插件's等待图像加载不起作用
- 简单的Jquery/Ajax加载不起作用
- 代码点火器&AJAX-Div重新加载不起作用
- HTML加载不起作用
- 有什么原因吗('img')加载不起作用(jQuery)
- 弹出加载不起作用
- 图像加载不起作用,错误Uncaught TypeError:类型错误
- 窗口加载不起作用
- 加载不起作用,但鼠标悬停正常工作
- 在wordpress中,ajax加载不起作用