在我的游戏中添加cookie
Adding cookies to my game
所以,我目前正在创建一个类似cookie点击器的游戏。我需要在JavaScript代码中添加cookie,这样在刷新页面时就不会丢失所有cookie。下面的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代码。
<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>
请考虑改用localStorage
。
window.localStorage.numberOfCookiesCollected = 123456;
重新加载页面
alert(window.localStorage.numberOfCookiesCollected); // 123456
相关文章:
- 单击时将类添加到 ancer 标签并使用 cookie 保存
- 添加/删除类并使用Cookie记住所选内容
- 在Protractor中添加Cookie以测试AngularJS
- 可以't从JSON在Phantomjs中添加cookie
- 添加Cookie以在jQuery移动页面中保持字体大小
- 向js fiddle添加cookie以保存用户键入的内容
- 如何在 Drupal7 中单击按钮时添加 cookie
- 在网页上加载闪光横幅后添加cookie
- 如何在java脚本中单击时添加cookie
- 无法通过iframe添加cookie
- jQuery,slideToggle-添加Cookie以记住状态
- 向10万用户添加cookie时需要考虑的性能问题
- 添加cookie以切换侧边栏
- Cookies html + js添加cookie不工作
- 如何在JMeter中修改/添加Cookie
- 添加cookie以拖放
- 为什么在请求远程脚本时添加cookie
- 在引导可折叠侧边栏导航中添加Cookie
- Javascript websocket客户端向报头添加cookie
- 在我的游戏中添加cookie