【问题标题】:Send and Retrieve high score from local storage从本地存储发送和检索高分
【发布时间】:2017-12-15 13:18:49
【问题描述】:

这是游戏的链接 https://jsfiddle.net/ab4gaf15/5/ 你必须注册并登录才能玩,我有这么多工作 我只需要知道该函数的外观,以便将玩家从游戏中获得的分数发送到本地存储,然后从本地存储中检索到一个包含所有玩家分数的表。

  /* This function is called when a logged in user 
    plays the game and gets a score */
function updateScore(newScore) {
    //Get the JavaScript object that holds the data for the logged in user
    var usrObj = JSON.parse(localStorage[localStorage.loggedInUser]);

    //Update the user object with the new top score
    /* NOTE YOU NEED TO CHANGE THIS CODE TO CHECK TO SEE IF THE NEW SCORE
        IS GREATER THAN THE OLD SCORE */
    usrObj.topscore = newScore;

    //Put the user data back into local storage.
    localStorage[localStorage.loggedInUser] = JSON.stringify(usrObj);
}


/* Loads the rankings table.
    This function should be called when the page containing the rankings table loads */
function showRankingsTable() {
    //Get a reference to the div that will hold the rankings table.
    var rankingDiv = document.getElementById("RankingsTable");

    //Create a variable that will hold the HTML for the rankings table
    var htmlStr = "";

    //Add a heading 
    htmlStr += "<h1>Rankings Table</h1>";

    //Add the table tag
    htmlStr += "<table>";

    //Work through all of the keys in local storage
    for (var key in localStorage) {
        //All of the keys should point to user data except loggedInUser
        if (key !== "loggedInUser") {
            //Extract object containing user data

            //Extract user name and top score
            htmlStr += "David";
            //Add a table row to the HTML string.
        }
    }

    //Finish off the table
    htmlStr += "</table>";

【问题讨论】:

  • localStorage 仅在客户端可用,因此用户无法获得其他人的排名。您需要从服务器中提取数据,以便人们查看彼此的排名。
  • 那我怎么能只为自己的分数做呢?

标签: javascript get set local store


【解决方案1】:

实际上,您已经获得了示例中所需的所有部分

这是在 localStorage 中设置值的示例...

localStorage[localStorage.loggedInUser] = JSON.stringify(usrObj);

这是再次检索该值的示例...

var usrObj = JSON.parse(localStorage[localStorage.loggedInUser]);

要在表格中显示当前用户的分数,应该如下所示...

/* Loads the rankings table.
    This function should be called when the page containing the rankings table loads */
function showRankingsTable() {
    //Get a reference to the div that will hold the rankings table.
    var rankingDiv = document.getElementById("RankingsTable");

    //Create a variable that will hold the HTML for the rankings table
    var htmlStr = "";

    //Add a heading
    htmlStr += "<h1>Rankings Table</h1>";

    //Add the table tag
    htmlStr += "<table>";

    //Work through all of the keys in local storage
    if (typeof localStorage[localStorage.loggedInUser] !== 'undefined') {

        usrObj = JSON.parse(localStorage[localStorage.loggedInUser]);
        htmlStr += "<tr>";
            htmlStr += "<td>";
                htmlStr += usrObj;
            htmlStr += "<td>";
                htmlStr += "<td>";
                    htmlStr += localStorage.loggedInUser;
                htmlStr += "<td>";
            htmlStr += "</td>";
        htmlStr += "</tr>";

    }

    //Finish off the table
    htmlStr += "</table>";
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-20
    • 2015-02-14
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 2018-11-04
    • 1970-01-01
    相关资源
    最近更新 更多