2017-12-15 30 views
0

htmlローカルストレージにゲームをプレイするときにプレーヤーが取得するスコアを追加しますか? どうすればこのことをやりますか? さらに、誰にもあまり問題にならないなら、どうすれば地元の店舗からスコアを取得してランキング表の中に入れることができますか?コードのほとんど はちょうど私が見カントいくつかのことは、私はあなたが書いたゲームを見たとき、私は本当に感動しました、プレーヤーのスコアをローカルストレージに送信するにはどうすればよいですか?

https://jsfiddle.net/gugui3z24/ab4gaf15/2/

/* 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

あなたは何を理解していませんか? – SLaks

+0

ユーザーがlocalstorageに得たスコアを送信する方法、次にlocalstorageからスコアを取得し、他のユーザーの得点が表示されている場所にランキングを入力してください –

答えて

0

をまず理解することは、そこにあります。それはきれいに見えます!

LocalStorageの仕組みを理解するために、LocalStorage API docsを読むことから始めます。次に、storage.jsのようなLocalStorage APIの使用を容易にするライブラリをチェックすることをお勧めします。ユーザーのスコアを取得するための責任を負うことになるあなたのコードは、(例えば)だろうstorage.jsで

// Set user score 
storage.set('score', 52.0); 
// Retrieve user score 
var storoage.get('score'); 
+0

あなたはそれをフィドルに追加できますか? –

+0

localStorage APIの基本を理解する必要があります。ワーキングコードはhttps://jsfiddle.net/ab4gaf15/5/ – Sagar

+0

@Sagarです。コードを試しましたが、ハイスコアがローカルストレージであることはわかりません。 –

関連する問題