javascript - 加载仅在单击按钮后有效
问题描述
我为我的增量游戏制作了“保存和加载”功能。
它们都可以工作,但是当我加载数据时,我必须单击一个按钮来“更新”这些值。这不是主要货币(scrap)的问题,而是变量collector、collectorCost、drone和droneCost的问题。
两个部分都有相同的代码,所以我不明白为什么它不应该自动更新。
单击按钮时调用“保存和加载”功能
我不知道我是否需要显示更多代码。我对这整个场景有点陌生:)。
编辑:在页面加载时尝试了load(),但这并没有改变任何东西。
<button onclick="save()">Save Game</button>
<button onclick="load()">Load Game</button>
function save(){
var save = {
scrap: scrap,
collector: collector,
collectorCost: collectorCost,
drone: drone,
droneCost: droneCost
};
localStorage.setItem("save",JSON.stringify(save));
};
function load(){
var savegame = JSON.parse(localStorage.getItem("save"));
if(typeof savegame.scrap !== "undefined") scrap = savegame.scrap;
if(typeof savegame.collector !== "undefined") collector = savegame.collector;
if(typeof savegame.collectorCost !== "undefined") collectorCost = savegame.collectorCost;
if(typeof savegame.drone !== "undifined") drone = savegame.drone;
if(typeof savegame.droneCost !== "undifined") droneCost = savegame.droneCost;
};
我希望这些数字会自动更新,这适用于主要货币,但不适用于:collector、collectorCost、drone和droneCost。
他们只有在我点击按钮购买任何一个之后才会改变
解决方案
推荐阅读
- javascript - 如何使Angular 7滚动到使用ngFor重复的组件
- c - 我对“3n+1 问题”有疑问。当我调试它时
- sql - SQL - 外部应用在 SQL Server 2000 中不起作用?
- deep-learning - 为什么 LSTM 单元中使用两种不同类型的激活函数?
- c - C 表达式中是否允许使用多个变量的副作用运算符?
- c++ - 项目代码同时正确和错误地注册“N”
- excel - 如何在 Excel 中创建因变量应用程序?
- sql-server - 为什么,何时何地我们将使用存储过程、视图和函数?
- ios - 斯威夫特 4 错误:[
setValue:forUndefinedKey:]:这个类对于键 emailField 不兼容键值编码 - ruby-on-rails - 当我在我的条带数量中使用变量时,为什么我不断收到错误消息?