2017-05-09 54 views
1
//Set Variables 
var cookies = 0; 
var cursors = 0; 
var prestige = 0; 
var cursorCookies = cursors * 0.1; 
var save = { 
    cookies: cookies, 
    cursors: cursors, 
    prestige: prestige 
} 

function loadgame(){ 
var savegame = JSON.parse(localStorage.getItem("save")); 
if (typeof savegame.cookies !== "undefined") cookies = savegame.cookies; 
if (typeof savegame.cursors !== "undefined") cursors = savegame.cursors; 
if (typeof savegame.prestige !== "undefined") prestige = savegame.prestige; 
} 
onload(loadgame()); 

function round(value, decimals) { 
    return Number(Math.round(value+'e'+decimals)+'e-'+decimals); 
} 

function CookieClick(number) 
{ 
    cookies = cookies + number; 
    document.getElementById("cookies").innerHTML = round(cookies,2) 
} 
function buyCursor(){ 
    var cursorCost = Math.floor(10 * Math.pow(1.1,cursors));  //works out the cost of this cursor 
    if(cookies >= cursorCost){         //checks that the player can afford the cursor 
     cursors = cursors + 1;         //increases number of cursors 
     cursorCookies = cursors * 0.1; 
     cookies = cookies - cursorCost;       //removes the cookies spent 
     document.getElementById('cursors').innerHTML = cursors; //updates the number of cursors for the user 
     document.getElementById('cookies').innerHTML = cookies; //updates the number of cookies for the user 
    } 
    var nextCost = Math.floor(10 * Math.pow(1.1,cursors));  //works out the cost of the next cursor 
    document.getElementById('cursorCost').innerHTML = nextCost; //updates the cursor cost for the user 
} 




window.setInterval(function(){ 
CookieClick(cursorCookies); 
localStorage.setItem("save",JSON.stringify(save)); 
document.getElementById('saveinfo').innerHTML = "Saved: " + Date(); 
}, 1000); 

這是我第一次用javascript和localStorage的工作,所以請多多包涵,謝謝:)問題與localStorage的裝載與JS

全部是裝載3個變量我但是有些東西並不正確。 我的工地:http://atz-exportforce320313.codeanyapp.com/

感謝您的幫助!

+1

_but東西在裏面你的東西不走right_這不是你的錯誤 – baao

+0

一個很好的說明它只是不加載。由於控制檯沒有給我任何反饋,所以我不能說真的有問題。對不起。這就是爲什麼我在這裏把我的測試聯繫起 – Exportforce

+0

控制檯在加載後直接在您的測試站點上顯示多個錯誤。 – CBroe

回答

1

正如我在Facebook的使者指出:

,如果你訪問一個尚未創建的localStorage的內部變量,.getItem()將簡單地返回undefined
如果你管undefinedJSON.parse()它只會返回null
要解決這個問題,您只需在嘗試使用前檢查JSON.parse()的響應。

一個簡單的例子:

var savegame = JSON.parse(localStorage.getItem('save')); 
if (savegame === null) { 
    // initialize default values here or simply abort further execution 
    return; 

    // if cookies, cursors and prestige are global variables you can access them through window: 
    var variables = ['cookies', 'cursors', 'prestige']; 

    variables.forEach(function(name) { 
     if (name in savegame) window[name] = savegame[name]; 
    }); 
} 

另外現在你正試圖通過這條線來保存狀態:
localStorage.setItem("save",JSON.stringify(save));
因爲你沒有寫你的變量納入save
你基本上至極將無法正常工作必須首先將它們從您的全局變量添加到一起,例如:

var variables = ['cookies', 'cursors', 'prestige']; 
variables.forEach(function(name) { 
    save[name] = window[name]; 
}); 
localStorage.setItem('save', JSON.stringify(save)); 

PS:
一般我不建議定義全球範圍內

0

好吧終於有了調試你的問題。你的代碼工作不正常的原因是,在第一次啓動時,你的本地存儲中絕對沒有任何東西。

因此,您必須首先檢查從localstorage檢索到的值是否爲空。

由於這裏的問題,您的setinterval根本沒有被調用,因此沒有存儲發生。

var savegame = JSON.parse(localStorage.getItem("save")); 
if (savegame !== null && typeof savegame.cookies !== "undefined") cookies = savegame.cookies; 
if (savegame !== null && typeof savegame.cursors !== "undefined") cursors = savegame.cursors; 
if (savegame !== null && typeof savegame.prestige !== "undefined") prestige = savegame.prestige; 
} 

而且如果你已經解決了你的第一個問題,仍然不知道爲什麼加載存儲顯示所有值0,那是因爲你從來沒有更新保存變量。做到這一點,你有你的代碼工作正常

+0

謝謝!不應該每隔一個打勾線更新_save_? – Exportforce

+0

這就取決於你。就我個人而言,如果有任何變化,我會更新保存。同樣更新我的本地存儲。 當它沒有任何正確的事情時,沒有使用調用相同的東西嗎? 如果問題已解決您的問題,請將其標記爲正確 – Sagar