0

我正在用 javascript 制作一个简单的游戏,我有兴趣使用 localstorage 存储和加载分数,而不是在刷新后重置它。

当前代码:

var points = 0;   
if (
        hero.x <= (monster.x + 30)
        && monster.x <= (hero.x + 30)
        && hero.y <= (monster.y + 30)
        && monster.y <= (hero.y + 30)
    ) {
        ++points;
    }

我存储点的最终结果应该是这样的,但我不太明白:

if (
        hero.x <= (monster.x + 30)
        && monster.x <= (hero.x + 30)
        && hero.y <= (monster.y + 30)
        && monster.y <= (hero.y + 30)
   ) {
        localStorage.points=Number(localStorage.points)+1;
     }
else
     {
        localStorage.points=0;
     }

我究竟做错了什么?

4

1 回答 1

0

您正在寻找的是:localStorage.setItem("points", localStorage.getItem("points")+1);

于 2013-03-04T14:38:21.013 回答