gpt4 book ai didi

javascript - 回访时本地存储未加载?

转载 作者:行者123 更新时间:2023-12-02 21:40:14 24 4
gpt4 key购买 nike

我正在制作一款浏览器游戏,但遇到了一个无论我如何努力都无法解决的问题。

点击绿色方 block 会增加计数器

并且控制台显示变量被保存到localStorage

但是返回网站后它会返回 0

下面是我的整个 JS 文件。唯一重要的部分在底部,您可以亲自前往https://sleepyshepherd.com查看。

//Saved Variables
let ashCount = 0;
let firetruck = 0;

//Initialize Game
window.onload = function() {
const realmTab = document.createElement("div");
const settingsTab = document.createElement("div");
const shopTab = document.createElement("div");
const statsTab = document.createElement("div");
const upgradesTab = document.createElement("div");
const burningBtn = document.createElement("div");
const enslavedTab = document.createElement("div");
const e1 = document.createElement("div");

realmTab.id = "realm";
upgradesTab.id = "upgrades";
settingsTab.id = "settings";
shopTab.id = "shop";
statsTab.id = "stats";
enslavedTab.id = "enslaved";
burningBtn.id = "burning";
e1.id = "e1";

const t1 = document.createTextNode("Realm");
realmTab.appendChild(t1); 
const t2 = document.createTextNode("Upgrades");
upgradesTab.appendChild(t2); 
const t3 = document.createTextNode("Shop");
shopTab.appendChild(t3); 
const t4 = document.createTextNode("Stats");
statsTab.appendChild(t4); 
const t5 = document.createTextNode("Settings");
settingsTab.appendChild(t5); 

document.getElementById("menu").appendChild(realmTab);
document.getElementById("menu").appendChild(upgradesTab);
document.getElementById("menu").appendChild(shopTab);
document.getElementById("menu").appendChild(statsTab);
document.getElementById("menu").appendChild(settingsTab);
document.getElementById("destroy").appendChild(burningBtn);
document.getElementById("destroy").appendChild(enslavedTab);
document.getElementById("enslaved").appendChild(e1);

//Menu Buttons
const viewport1 = document.querySelector(".viewport");

//Realm View Select
document.querySelector("#realm").addEventListener("click",  function() {
viewport1.id = "realmView";
});

//Upgrades View Select
document.querySelector("#upgrades").addEventListener("click",  function() {
viewport1.id = "upgradeView";
});

//shop View Select
document.querySelector("#shop").addEventListener("click",  function() {
viewport1.id = "shopView";
});

//stats View Select
document.querySelector("#stats").addEventListener("click",  function() {
viewport1.id = "statsView";
});

//settings View Select
document.querySelector("#settings").addEventListener("click",  function() {
viewport1.id = "settingsView";
});

// Destroy
function increaseAsh() {
ashCount += 1;
}

document.querySelector("#destroy").addEventListener("click", increaseAsh);

//Resources
const r1 = document.createElement("div");
r1.id = "r1";
document.querySelector("#resources").appendChild(r1);

const t6 = document.createElement("p");
r1.appendChild(t6);

window.setInterval(function() {
t6.textContent = ashCount;
}, 16);

//load game
let sg = JSON.parse(localStorage.getItem("save"));
sg.ashCount = ashCount;
}

// Save Game Interval
window.setInterval(function() {
let save = {
ashCount: ashCount,
firetruck: firetruck
}
localStorage.setItem("save", JSON.stringify(save));
}, 10000);

最佳答案

你写了sg.ashCount = ashCount;当你想要ashCount = sg.ashCount;

添加console.log(sg);

关于javascript - 回访时本地存储未加载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60382858/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com