我無法使本地存儲正常工作。這是一個使用javascript的小遊戲。這是我正在嘗試使用的保存和加載功能。 有沒有更好的方法來做到這一點? 要調用保存和加載功能,我正在使用按鈕。使用HTML保存對象5
編輯:我已編輯包含完整的代碼。
var snowballs = 0.0;
function userClick(number){
\t snowballs = snowballs + number;
\t 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
\t 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
\t }
\t 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;
\t 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
\t 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(){
\t snowballs = snowballs + 100
\t document.getElementById("snowballs").innerHTML = prettify(snowballs);
}
window.setInterval(function(){
\t
\t snowballClick(penguins);
\t userClick(igloos)
\t \t
}, 1000);
<html>
\t <head>
\t \t <link rel="stylesheet" type="text/css" href="interface.css" />
\t </head>
\t <body>
\t \t <button onclick="userClick(1)">Click Me!</button>
\t \t <br />
\t \t Snowballs: <span id="snowballs">0</span>
\t \t <br />
\t \t <button onclick="buyPenguin()">Buy Penguin</button>
\t \t <br />
\t \t Penguins: <span id="penguins">0</span>
\t \t <br />
\t \t Penguin Cost: <span id="penguinCost">10</span>
\t \t <br />
\t \t <button onclick="save()">Save!</button>
\t \t <br />
\t \t <button onclick="load()">Load!</button>
\t \t <br />
\t \t <button onclick="buyIgloos()">Buy Igloo</button>
\t \t <br />
\t \t Igloos: <span id="igloos">0</span>
\t \t <br />
\t \t Igloo Cost: <span id="iglooCost">10</span>
\t \t <button onclick="devGive()">dev give 100</button>
\t \t <script type="text/javascript" src="main.js"></script>
\t </body>
</html>
是您的代碼的工作呢?對象是否已成功保存並加載? – buhtla
嗨,那裏,除了保存和加載功能外,一切正常。當我嘗試調用保存或加載功能時,根本沒有任何反應......我不確定問題所在,我按照http://www.w3schools.com/html/html5_webstorage.asp上的說明操作。 我欣賞任何建議,謝謝。 – MIke
那麼是否有某種錯誤?從你的代碼,我會說保存/加載應該工作。 – buhtla