2017-12-15 26 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입니다. 코드를 시도했지만 최고 점수가 로컬 저장 장치인지 확인할 수 없습니까? –