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的值。由於某些原因,此代碼不是 工作請幫助。
你給出的代碼片段非常好。在其他地方尋找錯誤 – sabof
爲什麼變量maxScore是本地的? –