javascript 为本地存储添加高分
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29370017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding a high score to local storage
提问by superman123
I want to add a high score to my game. Right now I have this:
我想为我的游戏添加一个高分。现在我有这个:
var score = 0;
var highscore = 0;
The score variable works but whenever I get like points so total of 60 points it says the high score is still zero when checking it using console.log(highscore)
.
score 变量有效,但是每当我得到喜欢的分数时,总分为 60 分,它表示使用console.log(highscore)
.
I have this to store the high score:
我有这个来存储高分:
if (score > localStorage.getItem("highscore")) {
localStorage.setItem("highscore", score);
}
This does not work properly, but it also doesn't give any errors.
这不能正常工作,但它也没有给出任何错误。
回答by Discern
Try this:
试试这个:
var score = 0;
var highscore = localStorage.getItem("highscore");
if(highscore !== null){
if (score > highscore) {
localStorage.setItem("highscore", score);
}
}
else{
localStorage.setItem("highscore", score);
}
回答by Matias Fernandez Martinez
In addition to the parseInt
, you need to set the init value on the localStorage
first because localStorage.getItem("highscore",10)
doesn't returns 10 as default value if is not setted
除了parseInt
,您需要在第localStorage
一个上设置 init 值,因为localStorage.getItem("highscore",10)
如果未设置,则不会返回 10 作为默认值
var score = 0;
var highscore = 0;
localStorage.setItem("highscore",0);
Because when you check
因为当你检查
if (score > parseInt(localStorage.getItem("highscore"))) {
localStorage.setItem("highscore", score);
}
The localStorage.getItem("highscore")
will return null if the item is not setted yet. Or you can also do:
localStorage.getItem("highscore")
如果尚未设置该项目, 则将返回 null。或者你也可以这样做:
var storagedHighScore = localStorage.getItem("highscore");
if (storagedHighScore || score > parseInt(storagedHighScore)) {
localStorage.setItem("highscore", score);
}
回答by alan polar
I just reviewed your code in http://jsfiddle.net/unb8yLj3/and i couldn't find in what part you increase the score so when this part of code run score is 0 and localStorage.getItem("highscore") is 10 therefore it doesn't set highscore.
我刚刚在http://jsfiddle.net/unb8yLj3/ 中查看了您的代码,但我找不到您提高分数的部分,因此当这部分代码运行分数为 0 且 localStorage.getItem("highscore") 为 10 时因此它没有设置高分。
if (score > localStorage.getItem("highscore")) {
localStorage.setItem("highscore", score);
}
}
Is there another js file that does increase the score?
是否有另一个 js 文件确实增加了分数?
回答by radiaph
You need to retrieve the value from localStorage with
您需要从 localStorage 中检索值
highscore = +localStorage.getItem("highscore")
or some equivalent code. Items in localStorage
don't track the global variables with the same names.
或一些等效的代码。中的项目localStorage
不跟踪具有相同名称的全局变量。
回答by Vikash
if (score > parseInt(localStorage.getItem('highscore'), 10)) {
localStorage.setItem('highscore', score);
}
convert into int before comparing. its string
比较前先转换成int。它的字符串