[英]Adding cookies to my game
So, I'm currently creating a cookie clicker like game. 因此,我目前正在创建类似游戏的Cookie Clicker。 And I need to add cookies to my JavaScript code so I won't lose all of my cookies when refreshing the page. 而且我需要在我的JavaScript代码中添加cookie,以便刷新页面时不会丢失所有cookie。 JavaScript code down below. 下面的JavaScript代码。
var cookies = 0;
function cookieClick(number){
cookies = cookies + number;
document.getElementById("cookies").innerHTML = cookies;
};
var cursors = 0;
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
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
};
var grandmas = 0;
function buyGrandma(){
var grandmaCost = Math.floor(100 * Math.pow(1.1,grandmas)); //works out the cost of this grandma
if(cookies >= grandmaCost){ //checks that the player can afford the grandma
grandmas = grandmas + 1; //increases number of grandmas
cookies = cookies - grandmaCost; //removes the cookies spent
document.getElementById('grandmas').innerHTML = grandmas; //updates the number of grandmas for the user
document.getElementById('cookies').innerHTML = cookies; //updates the number of cookies for the user
};
var nextCost = Math.floor(100 * Math.pow(1.1,grandmas)); //works out the cost of the next grandma
document.getElementById('grandmaCost').innerHTML = nextCost; //updates the grandma cost for the user
};
window.setInterval(function(){
cookieClick(cursors);
}, 1000);
window.setInterval(function(){
cookieClick(grandmas);
}, 400);
HTML code down below. 下面的HTML代码。
<html>
<head>
</head>
<body>
<center>
<img src="http://hydra-media.cursecdn.com/minecraft.gamepedia.com/7/70/Cookie.png?version=3afa70a9c4eea17707300251844f3c1c" onclick="cookieClick(1)" />
<br />
Cookies: <span id="cookies">0</span>
<br />
<br />
<button onclick="buyCursor()">Buy Cursor</button>Cost: <span id="cursorCost">10</span>
<br />
Cursors: <span id="cursors">0</span><img src="http://png-4.findicons.com/files/icons/2566/horizon/16/cursor.png" />
<br />
<br />
<button onclick="buyGrandma()" title="Gives 4 cookies every second">Buy Grandma</button>Cost: <span id="grandmaCost">100</span>
<br />
Grandmas: <span id="grandmas">0</span><img src="http://img3.wikia.nocookie.net/__cb20130710043044/pokemontowerdefensetwo/images/1/18/Grandmum_icon.png" style="width:16px; height:20px;" />
<br />
<script type="text/javascript" src="main.js"></script>
<center>
</body>
</html>
Consider using localStorage
instead. 考虑改用localStorage
。
window.localStorage.numberOfCookiesCollected = 123456;
Reload the page 重新载入页面
alert(window.localStorage.numberOfCookiesCollected); // 123456
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.