2016-11-13 4 views
0

ユーザーがゲームループを完了するか新しいゲームを開始するときにすべてのローカルストレージをクリアしようとしていますが、同じ関数内でclear()を使用しながらlocalStorageオブジェクトを保持する

私はボリュームのための私の音値で、すでにこれを行うことができます。

// inside a conditional statement that fires when the user chooses to start a new game. 
if (newGameBool === '1') { 
    var tst = myAu; 
//myAu is the stored value that the user sets as sound using a range type input 

    localStorage.clear(); 

    localStorage.setItem("Au", tst);//A newly cleared localStorage just got a new value, and it's the same as it was before. 

    UI.myLoad();//reload the function that uses LS to do things. 

} 

は、私は彼らに添付反復番号を持っているキーのためにこれをどのように行うのですか?ここで

は、私はそれらを保存する方法である:

私はサウンド機能を行った方法それらを呼び出す
var i = +v + +1; 

localStorage.setItem("v", i); 

var vv = localStorage.getItem("v"); 

localStorage.setItem("LdrBrd_" + vv, JSON.stringify(LdrBrd));//saves all data with the iterating key name. 

var gv = v + 1;//v calls the value from LS and adjusted for off-by-one error. gv is a local variable. 
if (newGameBool === '1') { 
    var ldd, vg; 

    for (var ii = 0; ii < gv; ii++) { 
     var ld = localStorage.getItem("LdrBrd_" + ii); 
     if (ld != null) { 
     //these are the values that i want to pass beyond the clear point 
      ldd = JSON.parse(ld);//JSON string of data saved 
      vg = ii;//how many of them. 
     } 

    } 

    localStorage.clear(); 

    for (var xx = 0; xx < vg; xx++) { 
     var nld = localStorage.getItem("LdrBrd_" + xx); 
     if (nld != null) { 
      localStorage.setItem("LdrBrd_" + ii, JSON.stringify(ldd)); 
     } 
    } 
    localStorage.setItem("v", vg); 

    UI.myLoad(); 

} 

私が何を参照するために、様々なスポットにはconsole.log()を使用しています続行中です。私は、値が間違っていて、すべてをすべて保存していないかどうかを確認するためにクリア関数をコメントアウトします。私はフィドルを作ろうとしましたが、ローカルストレージはそこでは全く動作していませんでした。ビジュアルスタジオではうまくいきますが、このファイルへのスクリプトはほとんど2000行ですので、私はそれをどのように知っていたかを最高のものにするようにしました。

ご協力いただきありがとうございます。

+0

は、なぜあなたはそれを完全にクリアされている削除ではなく、保存された(下記参照)好きな方法でより多くの項目がある場合は、このオプションがベストです削除したいデータを削除するだけですか? –

+0

私は他のものをすべてクリアしたいので、削除したいアイテムが約20ほどあり、クリアポイントを超えてgamestateに保存して渡したいものがほんのわずかです – MrEhawk82

答えて

0

私は数日間これに固執しましたが、私はうまくいくと思ったので、後世の価値がある場合には私自身の質問に答えます。

locatStorage.clear(); 
/* ^LS clear() function is above all new setItem codes, some variables are declared globally and some are declared at the top of the functional scope or as param^ */ 
var itemClass = document.querySelectorAll(".itemClass");//the strings are here 

if (itemClass) {//make sure some exist 
    for (var p = 0; p < itemClass.length; p++) {//count them 
     mdd = JSON.parse(itemClass[p].innerText);//parse the data for saving 

     localStorage.setItem("v", v);//this is the LS item that saves the amount of items i have, it's declared at the top of the functions timeline. 
     localStorage.setItem("LdrBrd_" + p, JSON.stringify(mdd));//this setItem function will repeat and increment with 'p' and assign the right string back to the key name it had before. 
    } 
} 

キーは、文字列を物理的に要素に関連付けたままにして、クラス名を呼び出すことです。私はそれらを数えるループを走った。 'mdd'は、私が望む各項目を吐き出します。だから、それを元に戻すことだけが元の状態に戻ります。

これにより、ユーザーは新しいゲームを開始するときにlocalStorageをクリアした後でもトロフィーを収集して保持する方法を作成することができました。

私は文字列からテキストを隠すためにCSSを使用します。

color:transparent; 

私のgameLoopには、保存された文字列を読み込んで隠れた文字列のすぐ下にカードとして表示する関数があります。あなたは、私は2つのいずれかをお勧めしますいくつかの値を維持したいので

enter image description here

0

  1. localStorage.clear()を呼び出して、代わりにのみ、あなたがlocalStorage.removeItem('itemName')を使用して必要な値を一掃しないでください。アイテム名に数値コンポーネントがあると言ったので、コードを減らすためにループ内でこれを行うことができます。

  2. 最初に保存したいアイテムを引き出し、clear()を呼び出して復元します。あなたがあなたの代わりにいくつかのデータを保持したい場合は

function mostlyClear() { 
    var saveMe = {}; 
    saveMe['value1'] = localStorage.getItem('value1'); 
    saveMe['anotherValue'] = localStorage.getItem('anotherValue'); 
    localStorage.clear(); 
    for(var prop in saveMe) { 
     if(!saveMe.hasOwnProperty(prop)) continue; 

     localStorage.setItem(prop, saveMe[prop]); 
    } 
} 
関連する問題