我正在使用 HTML5 DOM 存储和 ECMAScript5 内置的 JSON 功能,使用辅助函数来存储和检索游戏状态,我的代码是:
function saveState(state) {
window.localStorage.setItem("gameState",state);
}
function restoreState() {
var state = window.localStorage.getItem("gameState");
if (state) {
return parse(state);
} else {
return null;
}
}
但无论如何我没有得到想要的输出,因为我是 JSON 新手,它很难解析。请帮忙!
最佳答案
尝试下面的代码:
function saveState(state) {
window.localStorage.setItem("gameState", JSON.stringify(state));
}
function restoreState() {
var state = window.localStorage.getItem("gameState");
if (state) {
return JSON.parse(state);
} else {
return null;
}
}
关于json - 使用 HTML5 DOM 存储和 JSON 存储和检索游戏状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20337910/