2017-12-15 32 views
0

からハイスコアを送信し、取得するには、ゲームへのリンクであるあなたは、私がちょうど何の機能を知っておく必要があり、そのくらい私は 作業持って、サインアップして再生するためにはログインする必要が 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

localstorageはクライアント側でしか利用できないため、他の人のランキングはユーザーに利用できません。人々がお互いのランキングを見るためには、サーバーからデータを引き出す必要があります。 – michael

+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>"; 
} 
関連する問題