所以我正在开发这个游戏,我遇到了保存系统的问题。当我尝试保存并加载游戏时,我将playerLevel
设为NaN
,而不是数字。我知道为什么会这样,这是因为输出是引号内的数字。我需要知道是否有任何方法可以将变量设置为等于被回调的同一变量。如果你不理解我的问题,这是我的代码:
function saveGame() {
localStorage.setItem("PLAYERLEVEL", playerLevel);
localStorage.setItem("PLAYERATTACK", Attack);
localStorage.setItem("PLAYERDEFENSE", Defense);
localStorage.setItem("PLAYERAGILITY", Agility);
localStorage.setItem("PLAYERCOINS", Coins);
localStorage.setItem("PLAYERHEALSLEFT", healsLeft);
localStorage.setItem("PLAYERPOSSHEALS", possHealsLeft);
localStorage.setItem("PLAYERLIVESLEFT", livesLeft);
};
function loadGame() {
playerLevel = localStorage.getItem("PLAYERLEVEL");
Attack = localStorage.getItem("PLAYERATTACK");
Defense = localStorage.getItem("PLAYERDEFENSE");
Agility = localStorage.getItem("PLAYERAGILITY");
Coins = localStorage.getItem("PLAYERCOINS");
healsLeft = localStorage.getItem("PLAYERHEALSLEFT");
possHealsLeft = localStorage.getItem("PLAYERPOSSHEALS");
livesLeft = localStorage.getItem("PLAYERLIVESLEFT");
};
提前感谢您的帮助!
答案 0 :(得分:2)
如果要在localStorage
中检索数值数据,则需要将值转换回数值(使用parseInt()或parseFloat())到您需要的数据类型,因为{ {1}}将所有内容存储为字符串,所以请执行以下操作:
localStorage