在本地存储中添加高分 [英] Adding a high score to local storage

查看:48
本文介绍了在本地存储中添加高分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在游戏中添加高分。现在我有这个:

I want to add a high score to my game. Right now I have this:

var score = 0;
var highscore = 0;

分数变量有效但每当我得到分数总共60分时它表示高分是使用 console.log(高分)检查时仍然为零。

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).

我有这个来存储高分:

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.

推荐答案

试试这个:

var score = 0;
var highscore = localStorage.getItem("highscore");

if(highscore !== null){
    if (score > highscore) {
        localStorage.setItem("highscore", score);      
    }
}
else{
    localStorage.setItem("highscore", score);
}

这篇关于在本地存储中添加高分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆