我在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;
}
我做错了什么?
答案 0 :(得分:0)
您要找的是:localStorage.setItem("points", localStorage.getItem("points")+1);