简体   繁体   English

我正在使用HTML5本地存储为用户制作保存游戏。 只是不起作用。 (JavaScript)

[英]I'm using HTML5 Local storage to make save games for users. It just is not working. (Javascript)

This is the tutorial I have been using. 这是我一直在使用的教程 It's right at the very bottom. 就在最底部。

The functions i'm using are: 我正在使用的功能是:

function saveGame(){
    printMessageLine("Saved.");
    localStorage['shittyInc_save'] = btoa(JSON.stringify(game));
}

function loadGame() {

    var save_data = get_cookie('shittyInc_save');
    printMessageLine("Loaded.");
    console.log(save_data);
    if (!save_data) return;
    console.log(save_data);
    game = save_data
}

the variable game looks like 可变游戏看起来像

    var game = {
     money: 0,
     xp: 0,
     level: 1,
     xpPimps: 0,
     slaves: 0,
     printers: 0,
     xpWhores: 0,
     slaveDrivers: 0,
     xpMines: 0,
     moneyTrees: 0,
     shittyInvestors: 0,
     shitMoney: 0,
     shittyCompanies: 0,
     moneyPigs: 0,
     shittyBitcoins: 0
};

the variable is used correctly and has been tested. 该变量已正确使用并经过测试。

I have a button that uses saveGame(); 我有一个使用saveGame();的按钮saveGame(); and loadGame(); loadGame();

<p><button onclick="saveGame()" style="background: #ccc url(triangular.png); padding: 0.2em 2em"">Save</button></p>
     <p><button onclick="loadGame()" style="background: #ccc url(triangular.png); padding: 0.2em 2em"">Load</button></p>

nothing seems to do anything, what am I missing? 似乎什么也没做,我想念什么?

Got It, fixed code below: 知道了,下面的固定代码:

  function loadGame() {

    var save_data = localStorage['shittyInc_save']
    printMessageLine("Loaded.");
    console.log(save_data);
    if (!save_data) return;
    console.log(save_data);

    game = JSON.parse(atob(localStorage['shittyInc_save']))
}

You get the data from localStorage the same way you stored it there, not with get_cookie . 您从localStorage获取数据的方式与在本地存储数据的方式相同,而不是使用get_cookie

var data = 'some data';
//save to localStorage
localStorage['myData'] = data;

//get from localStorage
var fromStorage = localStorage['myData']; //'some data'

Side note: inline javascript is bad practice and will cause you troubles. 旁注:内联JavaScript是一种不好的做法,会给您带来麻烦。 Read these results: https://www.google.com/search?q=Why+is+inline+js+bad%3F It is best practice to attach the event listener with addEventListener , like this: 阅读以下结果: https : //www.google.com/search?q= Why+is+ addEventListener最佳做法是将事件监听器附加到addEventListener ,如下所示:

Markup: 标记:

<button id="myButton"></button>

JavaScript: JavaScript:

var myButton = document.getElementById('myButton');
myButton.addEventListener('click', myClickFunction);

myClickFunction(event) {
  //do something
}

Here's a complete example demonstrating click functions with saving and loading from localStorage . 这是一个完整的示例,演示了单击功能以及如何从localStorage进行保存和加载。 Try it out by typing into the input and click "save". 通过输入内容进行尝试,然后单击“保存”。 You can then change the input or reload the page and get the value back by clicking "load". 然后,您可以更改输入或重新加载页面,然后单击“加载”以返回值。 Live demo here (click). 现场演示在这里(单击)。

Markup: 标记:

<input id="save-data" type="text" placeholder="type data here">
<button id="save">Save</button>
<button id="load">Load</button>

JavaScript: JavaScript:

var save = document.getElementById('save');
var load = document.getElementById('load');
var saveDataInput = document.getElementById('save-data');

save.addEventListener('click', function() {
  saveGame();
});

load.addEventListener('click', function() {
  loadGame();
});

function saveGame() {
  localStorage.saveData = saveDataInput.value;
}

function loadGame() {
  saveDataInput.value = localStorage.saveData;
}

The tutorial correctly uses the localStorage[] object for both saving and loading. 本教程正确使用localStorage[]对象进行保存和加载。

About 2/3 through http://blog.samgb.com/incrementals-from-zero-to-hero-part-3/ 大约2/3通过http://blog.samgb.com/incrementals-from-zero-to-hero-part-3/

We interface with local storage like this: 我们这样与本地存储接口:

Saving 保存

localStorage['clickclick_save'] = btoa(JSON.stringify(player));

Loading 载入中

player = JSON.parse(atob(localStorage['clickclick_save']))

The code you posted mismatches two different ways of storing and loading game data that are taught in the tutorial. 您发布的代码与本教程中介绍的两种不同的存储和加载游戏数据的方式不匹配。 It is as though you left your wallet under the bed and then looked for it in a drawer. 好像您将钱包放在床下,然后在抽屉里找它一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM