2014-12-25 60 views
0

我無法使本地存儲正常工作。這是一個使用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>

+0

是您的代碼的工作呢?對象是否已成功保存並加載? – buhtla

+0

嗨,那裏,除了保存和加載功能外,一切正常。當我嘗試調用保存或加載功能時,根本沒有任何反應......我不確定問題所在,我按照http://www.w3schools.com/html/html5_webstorage.asp上的說明操作。 我欣賞任何建議,謝謝。 – MIke

+0

那麼是否有某種錯誤?從你的代碼,我會說保存/加載應該工作。 – buhtla

回答

0

我認爲問題是,你沒有定義威信變量。

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

+0

好的,謝謝我仍然在學習,所以我很感謝幫助:P 我已將您的html代碼複製到我的html頁面和js aswell,現在看起來它自動加載當我刷新但立即價值去「南」?有任何想法嗎? – MIke

+0

我已更新我的anaswer,檢查出來,並再次檢查jsfiddle鏈接。如果這解決了您的問題,一定要將其標記爲答案。 – buhtla

+0

哦,上帝剛剛刪除了威望位,它與原始代碼一起工作......愚蠢的我,我正計劃添加下一個,但是當它沒有工作,我花了最後一個人工調試,並試圖使它保存...哈哈謝謝! – MIke