使用 HTML 5 保存对象

Save object using HTML 5

本文关键字:对象 保存 HTML 使用      更新时间:2023-09-26

我无法让这个本地存储工作。这是一个使用javascript的小游戏。这是我尝试使用的保存和加载功能。有没有更好的方法可以做到这一点?要调用保存和加载函数,我正在使用按钮。

编辑

:我已经编辑以包含完整的代码。

var snowballs = 0.0;
function userClick(number){
	snowballs = snowballs + number;
	document.getElementById("snowballs").innerHTML = prettify(snowballs);
}
function snowballClick(number){
    snowballs = snowballs + (number* 0.1) ;
    document.getElementById("snowballs").innerHTML = prettify(snowballs);
};
var penguins = 0.0;
var igloos = 0;
function buyPenguin(){
    var penguinCost = Math.floor(10 * Math.pow(1.1,penguins));     //works out the cost of this cursor
    if(snowballs >= penguinCost){                                   //checks that the player can afford the cursor
        penguins = penguins + 1;                                   //increases number of cursors
    	snowballs = snowballs - penguinCost;                          //removes the cookies spent
        document.getElementById('penguins').innerHTML = penguins;  //updates the number of cursors for the user
        document.getElementById('snowballs').innerHTML = snowballs;  //updates the number of cookies for the user
    };
    var nextCost = Math.floor(10 * Math.pow(1.1,penguins));       //works out the cost of the next cursor
    document.getElementById('penguinCost').innerHTML = nextCost;  //updates the cursor cost for the user
};
function save(){
    var save = {
        snowballs: snowballs,
        penguins: penguins,
        igloos: igloos,
        prestige: prestige
	}
	localStorage.setItem("save",JSON.stringify(save));
};
function load(){
    var savegame = JSON.parse(localStorage.getItem("save"));
    if (typeof savegame.snowballs !== "undefined") snowballs = savegame.snowballs;
    if (typeof savegame.penguins !== "undefined") penguins = savegame.penguins;
    if (typeof savegame.igloos !== "undefined") igloos = savegame.igloos;
    document.getElementById('penguins').innerHTML = penguins;
    document.getElementById('snowballs').innerHTML = snowballs;
    document.getElementById('igloos').innerHTML = igloos;
};
function prettify(input){
    var output = Math.round(input * 1000000)/1000000;
	return output;
}
function buyIgloos(){
    var iglooCost = Math.floor(10 * Math.pow(3,igloos));     //works out the cost of this cursor
    if(snowballs >= iglooCost){                                   //checks that the player can afford the cursor
        igloos = igloos + 1;                                   //increases number of cursors
    	snowballs = snowballs - iglooCost;                          //removes the cookies spent
        document.getElementById('penguins').innerHTML = penguins;  //updates the number of cursors for the user
        document.getElementById('snowballs').innerHTML = snowballs;  //updates the number of cookies for the user
        document.getElementById('igloos').innerHTML = igloos;
    };
    var nextIglooCost = Math.floor(10 * Math.pow(3,igloos));       //works out the cost of the next cursor
    document.getElementById('iglooCost').innerHTML = nextIglooCost;  //updates the cursor cost for the user
};




function devGive(){
	snowballs = snowballs + 100
	document.getElementById("snowballs").innerHTML = prettify(snowballs);
}

window.setInterval(function(){
	
	snowballClick(penguins);
	userClick(igloos)
		
}, 1000);
<html>
	<head>
		<link rel="stylesheet" type="text/css" href="interface.css" />
	</head>
	<body>
		<button onclick="userClick(1)">Click Me!</button>
		<br />
		Snowballs: <span id="snowballs">0</span>
		<br />
		<button onclick="buyPenguin()">Buy Penguin</button>
		<br />
		Penguins: <span id="penguins">0</span>
		<br />
		Penguin Cost: <span id="penguinCost">10</span>
		<br />
		<button onclick="save()">Save!</button>
		<br />
		<button onclick="load()">Load!</button>
		<br />
		<button onclick="buyIgloos()">Buy Igloo</button>
		<br />
		Igloos: <span id="igloos">0</span>
		<br />
		Igloo Cost: <span id="iglooCost">10</span>
		<button onclick="devGive()">dev give 100</button>
		<script type="text/javascript" src="main.js"></script>
	</body>
</html>

我认为问题是你没有定义声望变量。

var penguins = 0.0;
var igloos = 0;
var snowballs = 0.0;
var prestige = 'prestige'
function save(){
    var save = {
        snowballs: snowballs,
        penguins: penguins,
        igloos: igloos,
        prestige: prestige
    }
    localStorage.setItem("save",JSON.stringify(save));
};

看到这个链接,它将工作: jsfiddle