这是游戏的链接 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>";
答案 0 :(得分:0)
您实际上已经获得了示例中所需的所有内容
这是在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>";
}