2014-01-28 59 views
0
<!DOCTYPE HTML> 
<html> 


<head> 
    <title>Zautra Levels</title> 
    <h2 style="font-family: Trebuchet MS; color: blue;"">Zautra Levels</h2> 
    <p> </p> 
</head> 


<body> 

<p>Clickables:</p> 
<button id="swag" onclick="lmao()">Gain XP</button> <button id="gold" onclick="getgold()">Get Gold</button> <button id="buyupgrade" onclick="buyupp()">Level Up!</button> 
<p> </p> 
<div id="total">XP: 0</div> 
<div id="goldt">Gold: 0</div> 
<div id="upgradess">Level: 0</div> 
<div id="upcostt">Required XP: 25</div> 



<script> 
    var clicks = 0; // How many clicks you have 
    var upgrades = 0; // How many upgrades you have purchased 
    var upcost = 25; // How much the upgrades cost 
    var gold = 0; // How much gold you have 

    function updateclicks() { // Declares the function that updates the "Zautra Clicks" Text. 
     var v=document.getElementById("total"); 
     v.innerHTML = 'XP: ' + clicks; 
    } 

     function updategold() { // Declares the function that updates the "Zautra Clicks" Text. 
     var g=document.getElementById("goldt"); 
     g.innerHTML = 'Gold: ' + gold; 
    } 

    function updateupgradecounter() { // Declares the function that updates the "Upgrades:" Text. 
     var y=document.getElementById("upgradess"); 
     y.innerHTML = 'Level: ' + upgrades; 
    } 

    function updateupcost() { // Declares the function that updates the "Upgrade Cost:" Text. 
     var z=document.getElementById("upcostt"); 
     z.innerHTML = 'Required XP:' + upcost; 
    } 

    var x=document.getElementById("swag"); function lmao() { // When you click the "Click for MOAR Zautra's" Button you get a +1 Click. 
     clicks+=1; 
     updateclicks(); 
    } 

     var j=document.getElementById("gold"); function getgold() { // When you click the "Click for MOAR Zautra's" Button you get a +1 Click. 
     gold+=1; 
     updategold(); 
    } 

    var c=document.getElementById("buyupgrade"); function buyupp() { 
     if (clicks >= upcost) { 
      clicks-=upcost 
      upgrades+=1 
      upcost*=2 
      updateclicks(); 
      updateupgradecounter(); 
      updateupcost(); 
     } 
     else 
     { 
      var clicksdif = upcost - clicks; 
      confirm("You need " + clicksdif + " more XP to level up."); 
     } 
    } 
</script> 
</body> 
</html> 

這是我正在研究的遊戲的代碼。 我想添加一個按鈕,當你按下它時,它會保存所有的變量。 如果你的第5級有26 XP和7金幣,你刷新頁面,你仍然有這些統計數據,而不是在刷新時丟失它們。如何在刷新時保存變量?

請幫忙!

(而且是啊,我也知道代碼真的搞砸了,但是這是一個小問題,我會解決這個問題遲早的事。)

+0

使用cookies或者在現代瀏覽器[本地存儲](http://www.html5rocks.com/en/features/storage)中,如果您想要存儲的所有內容,客戶端 –

+0

只需在持久數據上使用localStorage命名空間:加載:var gold = + localStorage.gold;保存:localStorage.gold = gold; – dandavis

+0

你發佈到一個Java或asp.net的網頁或什麼? –

回答

0

我相信實際上最簡單的方法比Cookie更簡單,就是通過URL傳遞值。例如:

<form action="yourPage.php?gold=$amount&level=$whatlevel&experience=$experience" method="POST"> 

//這裏你的刷新按鈕

</form> 

,然後檢索這些變量時重新加載頁面,使用:黃金= $ _ POST [ '金']

另一種選擇以及使用GET方法而不是POST。 請記住,該代碼的工作文件擴展名需要爲PHP。

0

你可以在PHP中創建一個cookie:

setcookie("NameOfTheCookie",$value,$expireTime) 

$value也可以是值的數組。

+0

但我在哪裏告訴它要存儲什麼變量,以及它們的價值是什麼? – user3205119