我最近遇到了保存游戏的问题,经过一些进一步的摆弄后,我成功获得了localStorage();上班!不幸的是,当你刷新或重新打开游戏时,会发生一个令人讨厌的错误。我试图重新创建的游戏是Cookie Clicker。该错误如下:如果您购买了一个结构(I.E.自动点击器,每2秒+1饼干),它不会为您的总数添加一个cookie。相反,它会在Cookie总数的末尾添加1。例如,如果您有1004个Cookie,而不是为您带来1005个Cookie,则会生成10041个Cookie。而这只是继续前进。结构成本也是如此。例如,(注意我增加成本的公式是:成本+ =成本* .3)如果您购买50个cookie的第一个自动点击器,下一个自动点击器需要5015个cookie而不是65个cookie。我不知道为什么会这样。任何解释都表示赞赏。
<!DOCTYPE html>
<html>
<head>
<title>Cookie Clickers!</title>
<link type="stylesheet/css" rel="stylesheet" href="style.css" />
<script language="javascript">
</script>
</head>
<body>
<h1>Cookie Clickers By: Michael</h1>
<h3>Original Idea By: Orteil</h3>
<br>
<br>
<h1 id="CookieAmount"></h1>
<h2 id="CookiePerSecond"></h2>
<div id="cookie" style="cursor:pointer" onclick="cookieClicked();">
<img src="http://img1.wikia.nocookie.net/__cb20130827014912/cookieclicker/images/thumb/5/5a/PerfectCookie.png/250px-PerfectCookie.png">
</div>
<!--Tells player how much Auto Clicker Costs-->
<button onclick="getAutoClicker();">Purchase Auto Clicker for <span id="AutoClickCookieCost"></span>
<br><span id="AutoClickTotal"></span> owned</button>
<br>
<button onclick="saveGame();">Save Game</button>
<button onclick="resetConfirm();">Reset Game</button>
<div>
<script language="javascript">
//Checks if variables are defined
if(typeof cookieClicks === "undefined"){
//If no variables are defined, the variables are defined and given a default value.
var cookieClicks = 0;
}
var cookieClicks = localStorage.cookieClicks;
if(typeof clckValue === "undefined"){
//If no variable is defined, variable is given value.
var clickValue = 1;
}
var clickValue = localStorage.clickValue;
if(typeof AutoClickers === "undefined"){
//If no Auto Clickers defined, defaul value given.
var AutoClickers = 0;
}
var AutoClickers = localStorage.AutoClickers;
if(typeof AutoClickerCost === "undefined"){
//If no Auto Clicker Cost defined, default value given.
var AutoClickerCost = 50;
}
var AutoClickerCost=localStorage.AutoClickerCost;
//==================================End of Variables========================================================================================
//==================================Begin Compute Structure CPS=============================================================================
//Purchases Auto Clicker for Player and removes Cookies.
function getAutoClicker() {
if (cookieClicks >= AutoClickerCost) {
AutoClickers++;
cookieClicks -= AutoClickerCost;
AutoClickerCost += Math.floor(AutoClickerCost *.3);
} else {
alert("Oh No! It appears you do not have enough cookies to purchase an Auto Clicker. An Auto Clicker currently costs " + AutoClickerCost + " which is " + (AutoClickerCost - cookieClicks) + " more cookies than you have.")
}
}
//Rests Game
function resetConfirm(){
var answer = confirm("Are you sure you wish to reset? ALL progress will be lost FOREVER.");
if(answer){
cookieClicks = 0;
clickValue = 0;
AutoClickers = 0;
AutoClickerCost = 50;
}
}
//Processes cookie click and adds it to total.
function cookieClicked(){
cookieClicks++;
}
//Main Game loop updating Cookie Totals from AUTOCLICKER ONLY!
var AutoClickTimer = setInterval(function() {
AutoClickCookie()
}, 2000);
function AutoClickCookie() {
cookieClicks = cookieClicks+parseInt((AutoClickers*1),10);
}
//Side Loop updating button costs and CPS
var CookieClickTimer = setInterval(function() {
updateCookie()
}, 100);
function updateCookie() {
//Calculate CPS
document.getElementById("CookieAmount").innerHTML = cookieClicks + " cookies.";
//CPS PlaceHolder;
document.getElementById("AutoClickCookieCost").innerHTML = AutoClickerCost;
document.getElementById("AutoClickTotal").innerHTML = AutoClickers;
}
var saveGameLoop = setInterval(function(){saveGame()}, 100);
function saveGame(){
localStorage.cookieClicks = cookieClicks;
localStorage.clickValue = clickValue;
localStorage.AutoClickers = AutoClickers;
localStorage.AutoClickerCost = AutoClickerCost;
};
</script>
</div>
</body>
</html>
&#13;
谢谢, 迈克尔
答案 0 :(得分:2)
我没有对您的所有代码进行筛选,但在阅读完您的描述后,我想您的问题是您没有将字符串数据转换为数字。请记住,localStorage仅存储字符串值。所以,当你需要一个数字时,我建议你使用内置的parseInt函数。
var cost = parseInt(localStorage.AutoClickerCost, 10);
// 10 means base 10 here (optional, but helpful)
答案 1 :(得分:0)
试试这个AutoClickCookie()
而不是AutoClickCookie()
函数:
function AutoClickCookie() {
cookieClicks = parseInt(cookieClicks)+parseInt((AutoClickers*1),10);
}
答案 2 :(得分:0)
将 Number(); 标记添加到您获得更大数字的地方(1004)
Number(<code for getting number>) + <other number> = variable