2014-03-18 26 views
0
if (typeof localStorage["BestScore"] <= "undefined") 
{ var maxScore= localStorage["BestScore"] } 
} else  localStorage["BestScore"] = 0; 
    var maxScore=0; 

    var newScore=false 
    function drawScore(score) { 
    if (newScore == true && score < maxScore) { 
     newScore = false; 
    } 
if (score > maxScore) { 
    newScore = true; 
    localStorage["BestScore"] = score; 
    if ([5, 10, 15, 20].indexOf(score) !== -1) { 
    play(sndMedal); 
    } else { 
    play(sndGain); 
    } 
} 
maxScore = Math.max(score, maxScore); 

ctx.drawImage(sheet, 146, 58, 113, 58, 
       -226, 0, 226, 116);  
ctx.save(); 
ctx.translate(-20, 45); 
ctx.scale(0.5, 0.5); 
var size = drawNbr(score, false); 
ctx.restore(); 

// var draw medals 
ctx.save(); 
ctx.translate(-178, 66); 
if (score >= 20) { // platinum 
    ctx.drawImage(sheet,220, 144, 22, 22, -22, -22, 44, 44);  
} else if (score >= 15) { // gold 
    ctx.drawImage(sheet,242, 229, 22, 22, -22, -22, 44, 44); 
} else if (score >= 10) { // silver 
    ctx.drawImage(sheet,266, 229, 22, 22, -22, -22, 44, 44); 
} else if (score >= 5) { // bronze 
    ctx.drawImage(sheet,302, 137, 22, 22, -22, -22, 44, 44); 
} 

ctx.restore(); 

if (newScore) { // draw NEW 
    ctx.save(); 
    ctx.translate(-60 - (size*16), 37); 
    ctx.drawImage(sheet, 146, 245, 16, 7, 
       0, 0, 32, 14);  
    ctx.restore(); 
} 

ctx.save(); 
ctx.translate(-20, 88); 
ctx.scale(0.5, 0.5); 
drawNbr(maxScore, false); 
ctx.restore(); 

}本地存儲未定義/沒有價值但不工作

這是當我運行它時,我有maxscore會發生什麼=的 本地存儲[imageshack.com/ a/img691/167/28x1.png] [1],它被凍結。

此代碼試圖檢查本地存儲的最好成績是 未定義/沒有價值,如果它是它設置maxscore爲0;如果它有一個 值其設置maxscore的值。由於某些原因,此代碼不是 工作請幫助。

[1]:http://imageshack.com/a/img691/167/28x1.png

+0

你給出的代碼片段非常好。在其他地方尋找錯誤 – sabof

+0

爲什麼變量maxScore是本地的? –

回答

-1

試試這個:

if (typeof localStorage.BestScore === 'undefined') { 
    localStorage.BestScore = 0; 
    var maxScore=0; 
}  
else { 
    var maxScore=localStorage.BestScore; 
} 
0

首先檢查Storage是可以或不可以然後檢查你的localStorage BestScore對象是有或沒有。

if (typeof (Storage) !== "undefined") { 
    if (!localStorage.BestScore) { 
    localStorage.BestScore = 0; 
    } 
    var maxScore = localStorage.BestScore; 
} 
相關問題