简体   繁体   English

尝试将类中的数据保存到localStorage

[英]Trying to save data from a class into localStorage

I'm very new to programming, very new to Javascript. 我对编程非常陌生,对Java也非常陌生。 The professor is not any help, and I'm doing this with a friend (who is also a beginner) and we're both stuck. 教授没有任何帮助,我正在和一个朋友(也是一个初学者)一起做这件事,我们都被困住了。

We're trying to make a simple game and we're told that we have to use a save and loading feature using localStorage. 我们正在尝试制作一个简单的游戏,并被告知必须使用localStorage的保存和加载功能。

My friend handles the game logic while and I'm doing the save and load. 我的朋友在处理游戏逻辑的同时,我正在进行保存和加载。

Problem is, I can't figure out how to get the game and player data that I need to put into an object literal that i can stringify and save it into localStorage. 问题是,我无法弄清楚如何获取需要放入对象文字中的游戏和玩家数据,我可以将其字符串化并保存到localStorage中。

Every time I inspect element in firefox, it says no data present for selected host. 每次检查firefox中的元素时,都会显示所选主机没有数据。

This is my friend's code: 这是我朋友的代码:


class Game {
    constructor(level, score) 
    {
        // game logic truncated to not clutter..

        this.levelNum = level;

        this.total = score;
        this.hits = 0;
        this.misses = 0;
        this.time = 0;
    }
    // Game methods

    // truncated...
}

class Player
{
    constructor(fName, playerAge)
    {
        this.firstName = fName;
        this.age = playerAge;
    }
    displayInfo()
    {
        return `${this.firstName}, AGE ${this.age}`;
    }
}

// Creating player one
function createPlayer ()
{
    let playerName = document.querySelector("#textbox").value;
    let playerAge = parseInt(document.querySelector("#selectBox").value);

    return new Player(playerName, playerAge);
}
let playerOne = createPlayer();
clickPlay.addEventListener("click", createPlayer());

var playGame;
var gameLevel = 1;

function showLevel()
{
    playGame = new Game(gameLevel);
    let currentLevel = document.querySelector("#levelNum");
    let levelName = document.querySelector("#mathType");
    let playerID = document.querySelector("#playerName");
    let ageID = document.querySelector("#playerAge");

    currentLevel.innerHTML = `${playGame.levelNum}`;
    levelName.innerHTML = `${playGame.mathType}`;
    playerID.innerHTML = `${playerOne.firstName}`;
    ageID.innerHTML = `${playerOne.age}`;

}

clickPlay.addEventListener("click", showLevel());

This is what I've tried and I inserted this at the very end of the above code: 这是我尝试过的方法,我在上面的代码结尾处插入了此代码:

let save_button = document.querySelector("#savBut");

function createUserData(){
  var userData =
  {
    nName: playerOne.firstName,
    nAge: playerOne.age,
    nLevel: playGame.levelNum,
    nMisses: playGame.misses,
    nHits: playGame.hits,
    nSecs: playgame.time
  };
}

clickPlay.addEventListener("click", createUserData();

function saving() {
    // stringify first before storing to userInfo
  localStorage.setItem("userInfo", JSON.stringify(userData));
}

save_button.addEventListener("click", saving);

instead of creating the userData on start of the game, you should create it periodically. 而不是在游戏开始时创建userData,而是应定期创建它。 For testing I would create it when you click the save button and console out the userData to see if the data is in your object before saving. 为了进行测试,我将在您单击保存按钮时创建它,并在保存之前控制台出userData来查看数据是否在对象中。 I would also suggest naming the keys the same as from the objects they came from to make loading the data back easier. 我还建议为键命名与它们来自的对象相同,以使加载数据更容易。

function saving() {
  // stringify first before storing to userInfo
  const userData =
  {
    firstName: playerOne.firstName,
    age: playerOne.age,
    levelNum: playGame.levelNum,
    misses: playGame.misses,
    hits: playGame.hits,
    time: playgame.time
  };
  console.log(userData);
  localStorage.setItem("userData", JSON.stringify(userData));
}

you have probably already been saving your data correctly and it will show up in the developer console under the main tab Application in the local storage list. 您可能已经正确保存了数据,数据将显示在本地存储列表中主选项卡“ Application下的开发人员控制台中。

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

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