Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用javascript localstorage存储和加载数据?_Javascript_Html5 Canvas_Local Storage - Fatal编程技术网

使用javascript localstorage存储和加载数据?

使用javascript localstorage存储和加载数据?,javascript,html5-canvas,local-storage,Javascript,Html5 Canvas,Local Storage,我正在用javascript制作一个简单的游戏,我感兴趣的是使用localstorage存储和加载分数,而不是刷新后重置 当前代码: var points = 0; if ( hero.x <= (monster.x + 30) && monster.x <= (hero.x + 30) && hero.y <= (monster.y + 30) && mons

我正在用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;
    }
var点=0;
如果(

hero.x您要查找的是:
localStorage.setItem(“points”,localStorage.getItem(“points”)+1);

建议使用set/get方法而不是直接属性访问,但不是必需的。
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;
     }