将我的游戏分数存储在本地存储中

时间:2017-12-14 15:50:00

标签: javascript local-storage

我对JS很新,所以我在这里挣扎。基本上我在我的JavaScript中有一个得分函数,我想在本地存储它,以便以后可以通过另一个页面检索它。我使用本地存储进行登录和注册页面,但我不确定如何为得分做同样的事情。

add_row_of_pipes: function() {
    var hole = Math.floor(Math.random()*5)+1;

    for (var i = 0; i < 8; i++)
        if (i != hole && i != hole +1) 
            this.add_one_pipe(400, i*60+10);   

    this.score += 1;
    this.label_score.content = this.score;
};  

所有反馈都将受到高度赞赏:)

2 个答案:

答案 0 :(得分:1)

这是您可以在localStorage中存储和检索值的方法:

//商店

localStorage.setItem("score", "100");

//检索

var score = localStorage.getItem("score");

答案 1 :(得分:0)

这是一个我把它放在一起的小型LocalStorage框架。 它涵盖了localstorage所需的所有明显功能。

 function set_LocalStorage(key,value)
{
 //localStorage.setItem("name of variable", "value to store");
   localStorage.setItem(key, value);
   console.log('LocalStorage: ' + key + ' has been set to: ' + value);
}//End set_LocalStorage


function get_LocalStorage(key)
{
  return localStorage.getItem(key);
  console.log('LocalStorage: ' + key + ' has a value of: ' + value);
}//End get_LocalStorage


function remove_LocalStorage(key)
{
  localStorage.removeItem(key);
  console.log('LocalStorage: ' + key + ' has been removed');
}//End remove_LocalStorage


function check_LocalStorage_exist(key)
{
  var v = get_LocalStorage(key); 
  var v2 = toInteger(v);
  var FeedBack;

  if(v2 == 'null' || v2 === 'NaN' || v2 == 'undefined' || v2 == ''){ /*console.log('key '+key+' does NOT exist');*/ FeedBack='NO';}
  if(v2!=0){ 
      console.log('key '+key+' exist');
        FeedBack='YES';
    }

  return FeedBack;
}//End check_LocalStorage




function list_All_LocalStorage()
{
  for (var i = 0; i < localStorage.length; i++)
   {
    let item = localStorage.getItem(localStorage.key(i));   //--Will only need to have this on when collecting APP_DATA
    console.log('------ LocalStorage: '+localStorage.key(i)+' = '+item); 
   }

}//End list_All_LocalStorage



function remove_All_LocalStorage()
{
  for (var i = 0; i < localStorage.length; i++)
   {
    let s_key = localStorage.key(i);
    remove_LocalStorage(s_key);
   }

}//End remove_All_LocalStorage

欢迎你! :)