Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 本地存储未定义/无值,但不';行不通 if(本地存储的类型[“BestScore”]maxScore){ newScore=true; 本地存储[“最佳分数”]=分数; 如果([5,10,15,20].indexOf(分数)!=-1){ 播放(sndMedal); }否则{ 播放(sndGain); } } maxScore=Math.max(分数,maxScore); ctx.图纸图像(活页146、58、113、58、, -226, 0, 226, 116); ctx.save(); ctx.translate(-20,45); ctx.刻度(0.5,0.5); 变量大小=提款NBR(分数,假); ctx.restore(); //他赢得了奖牌 ctx.save(); ctx.translate(-178,66); 如果(分数>=20){//白金 ctx.drawImage(活页,220、144、22、22、-22、-22、44、44); }如果(分数>=15){//gold ctx.drawImage(第242、229、22、22、-22、-22、44、44页); }否则,如果(分数>=10){//silver ctx.drawImage(活页26622922-22-22-22-44); }如果(分数>=5){//铜牌 ctx.drawImage(活页302137,22,22,-22,-22,44,44); } ctx.restore(); if(newScore){//绘制新的 ctx.save(); ctx.translate(-60-(大小*16),37); ctx.图纸图像(第146页、第245页、第16页、第7页、, 0, 0, 32, 14); ctx.restore(); } ctx.save(); ctx.translate(-20,88); ctx.刻度(0.5,0.5); drawNbr(maxScore,假); ctx.restore();_Javascript_Html_Local Storage - Fatal编程技术网

Javascript 本地存储未定义/无值,但不';行不通 if(本地存储的类型[“BestScore”]maxScore){ newScore=true; 本地存储[“最佳分数”]=分数; 如果([5,10,15,20].indexOf(分数)!=-1){ 播放(sndMedal); }否则{ 播放(sndGain); } } maxScore=Math.max(分数,maxScore); ctx.图纸图像(活页146、58、113、58、, -226, 0, 226, 116); ctx.save(); ctx.translate(-20,45); ctx.刻度(0.5,0.5); 变量大小=提款NBR(分数,假); ctx.restore(); //他赢得了奖牌 ctx.save(); ctx.translate(-178,66); 如果(分数>=20){//白金 ctx.drawImage(活页,220、144、22、22、-22、-22、44、44); }如果(分数>=15){//gold ctx.drawImage(第242、229、22、22、-22、-22、44、44页); }否则,如果(分数>=10){//silver ctx.drawImage(活页26622922-22-22-22-44); }如果(分数>=5){//铜牌 ctx.drawImage(活页302137,22,22,-22,-22,44,44); } ctx.restore(); if(newScore){//绘制新的 ctx.save(); ctx.translate(-60-(大小*16),37); ctx.图纸图像(第146页、第245页、第16页、第7页、, 0, 0, 32, 14); ctx.restore(); } ctx.save(); ctx.translate(-20,88); ctx.刻度(0.5,0.5); drawNbr(maxScore,假); ctx.restore();

Javascript 本地存储未定义/无值,但不';行不通 if(本地存储的类型[“BestScore”]maxScore){ newScore=true; 本地存储[“最佳分数”]=分数; 如果([5,10,15,20].indexOf(分数)!=-1){ 播放(sndMedal); }否则{ 播放(sndGain); } } maxScore=Math.max(分数,maxScore); ctx.图纸图像(活页146、58、113、58、, -226, 0, 226, 116); ctx.save(); ctx.translate(-20,45); ctx.刻度(0.5,0.5); 变量大小=提款NBR(分数,假); ctx.restore(); //他赢得了奖牌 ctx.save(); ctx.translate(-178,66); 如果(分数>=20){//白金 ctx.drawImage(活页,220、144、22、22、-22、-22、44、44); }如果(分数>=15){//gold ctx.drawImage(第242、229、22、22、-22、-22、44、44页); }否则,如果(分数>=10){//silver ctx.drawImage(活页26622922-22-22-22-44); }如果(分数>=5){//铜牌 ctx.drawImage(活页302137,22,22,-22,-22,44,44); } ctx.restore(); if(newScore){//绘制新的 ctx.save(); ctx.translate(-60-(大小*16),37); ctx.图纸图像(第146页、第245页、第16页、第7页、, 0, 0, 32, 14); ctx.restore(); } ctx.save(); ctx.translate(-20,88); ctx.刻度(0.5,0.5); drawNbr(maxScore,假); ctx.restore();,javascript,html,local-storage,Javascript,Html,Local Storage,} 这就是当我运行它时,当我有maxscore= 本地存储[imageshack.com/a/img691/167/28x1.png][1],并将其冻结 此代码尝试检查本地存储最佳分数是否为 未定义/无值,如果是,则在有 值将maxscore设置为该值。由于某些原因,此代码不是 请帮忙 [1] : 试试这个: if (typeof localStorage["BestScore"] <= "undefined") { var maxScore= localStorage["BestSco

}

这就是当我运行它时,当我有maxscore= 本地存储[imageshack.com/a/img691/167/28x1.png][1],并将其冻结

此代码尝试检查本地存储最佳分数是否为 未定义/无值,如果是,则在有 值将maxscore设置为该值。由于某些原因,此代码不是 请帮忙

[1] :

试试这个:

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();

首先检查
Storage
是否可行,然后检查本地存储
BestScore
对象是否存在

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

你给出的片段非常好。查找其他错误为什么变量maxScore是本地的?
if (typeof (Storage) !== "undefined") {
    if (!localStorage.BestScore) {
    localStorage.BestScore = 0;
    }
    var maxScore = localStorage.BestScore; 
}