如何在本地存储中保存游戏分数
How to save game score in local storage
我想在本地存储中保存游戏分数。
我试过了
localStorage.setItem("score", myScore);
和
var score = localStorage.getItem(myScore);
想保存游戏比分但是没用。我做错了吗?我该怎么办?
function updateGameArea() {
var x, height, gap, minHeight, maxHeight, minGap, maxGap;
for (i = 0; i < myObstacles.length; i += 1) {
if (myGamePiece.crashWith(myObstacles[i])) {
return;
}
}
myGameArea.clear();
myGameArea.frameNo += 1;
if (myGameArea.frameNo == 1 || everyinterval(150)) {
x = myGameArea.canvas.width;
minHeight = 20;
maxHeight = 200;
height = Math.floor(Math.random()*(maxHeight-minHeight+1)+minHeight);
minGap = 50;
maxGap = 200;
gap = Math.floor(Math.random()*(maxGap-minGap+1)+minGap);
myObstacles.push(new component(10, height, "green", x, 0));
myObstacles.push(new component(10, x - height - gap, "green", x, height + gap));
}
for (i = 0; i < myObstacles.length; i += 1) {
myObstacles[i].x += -1;
myObstacles[i].update();
}
myScore.text="SCORE: " + myGameArea.frameNo;
myScore.update();
myGamePiece.newPos();
myGamePiece.update();
}
localStorage.setItem("score", myScore);
var score = localStorage.getItem(myScore);
关键是"score"
不是myScore
localStorage.setItem("score", myScore);
let score = localStorage.getItem("score"); // <--- key is "score", not myScore
我想在本地存储中保存游戏分数。 我试过了
localStorage.setItem("score", myScore);
和
var score = localStorage.getItem(myScore);
想保存游戏比分但是没用。我做错了吗?我该怎么办?
function updateGameArea() {
var x, height, gap, minHeight, maxHeight, minGap, maxGap;
for (i = 0; i < myObstacles.length; i += 1) {
if (myGamePiece.crashWith(myObstacles[i])) {
return;
}
}
myGameArea.clear();
myGameArea.frameNo += 1;
if (myGameArea.frameNo == 1 || everyinterval(150)) {
x = myGameArea.canvas.width;
minHeight = 20;
maxHeight = 200;
height = Math.floor(Math.random()*(maxHeight-minHeight+1)+minHeight);
minGap = 50;
maxGap = 200;
gap = Math.floor(Math.random()*(maxGap-minGap+1)+minGap);
myObstacles.push(new component(10, height, "green", x, 0));
myObstacles.push(new component(10, x - height - gap, "green", x, height + gap));
}
for (i = 0; i < myObstacles.length; i += 1) {
myObstacles[i].x += -1;
myObstacles[i].update();
}
myScore.text="SCORE: " + myGameArea.frameNo;
myScore.update();
myGamePiece.newPos();
myGamePiece.update();
}
localStorage.setItem("score", myScore);
var score = localStorage.getItem(myScore);
关键是"score"
不是myScore
localStorage.setItem("score", myScore);
let score = localStorage.getItem("score"); // <--- key is "score", not myScore