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]并冻结。
此代码尝试检查本地存储最佳分数是否为 undefined / no value如果是,则将maxscore设置为0(如果有) 值将maxscore设置为该值。出于某种原因,这段代码不是 工作请帮忙。
答案 0 :(得分:0)
首先检查Storage
是否可行,然后检查您的localStorage BestScore
对象是否存在。
if (typeof (Storage) !== "undefined") {
if (!localStorage.BestScore) {
localStorage.BestScore = 0;
}
var maxScore = localStorage.BestScore;
}
答案 1 :(得分:-1)
试试这个:
if (typeof localStorage.BestScore === 'undefined') {
localStorage.BestScore = 0;
var maxScore=0;
}
else {
var maxScore=localStorage.BestScore;
}