我从简单的增量浏览器游戏开始,你基本上只需点击一个按钮,数字计数器就会上升,这个数字就变成了可以用来购买升级的货币,以便数字计数器上升本身。
我唯一想弄清楚的是如何创建保存功能/将所有数据保存到localStorage,以便在刷新页面或关闭浏览器时,数字不会重置为0
以下是我试图存储的javascript代码的一部分:
var cookies = 0;
function cookieClick(number){
cookies = cookies + number;
document.getElementById("cookies").innerHTML = cookies;
};
var cursor = 0;
function buyCursor(){
var cursorCost = Math.floor(10 * Math.pow(1.1,cursor));
if(cookies >= cursorCost){
cursor = cursor + 1;
cookies = cookies - cursorCost;
document.getElementById('cursor').innerHTML = cursor;
document.getElementById('cookies').innerHTML = cookies;
};
var nextCost = Math.floor(10 * Math.pow(1.1,cursor));
document.getElementById('cursorCost').innerHTML = nextCost;
};
window.setInterval(function(){
cookieClick(cursor);
}, 1000);
这是HTML代码:
<body bgcolor="#2E2E2E"> <div style="text-align: center"> <button onclick="cookieClick(1)"><font size=4>Click</font></button> <br /> <font size="5" color="red">Cash: </font> <span id="cookies">0</span> <div> <br /> <DIV style="position: absolute; top:370px; left:230px; width:300px; height:20px"> <button onclick="buyCursor()">Buy Cursor</button> <br /> Cursors: <span id="cursor">0</span> <br /> Cursor Cost: <span id="cursorCost">10</span> <script type="text/javascript" src="main.js"></script> </body> </html>
这是我尝试将数据存储到localStorage的内容(但显然不起作用):
<script>
// Check browser support
if (typeof(Storage) != "undefined")
// Store
localStorage.cookies;
// Retrieve
{
document.getElementById("cookies").innerHTML=localStorage.cookies;
}
else
{
document.getElementById("cookies").innerHTML="Sorry, your browser does not support Web Storage...";
}
</script>
所以我的问题是,如何使用我正在使用的代码来处理这个问题?