Reputation: 17
I recently had a problem with saving my game, and after some further fiddling around, I successfully got localStorage(); to work! Unfortunately, when you do refresh or reopen the game, a nasty bug occurs. The game I am trying to recreate is Cookie Clicker. The bug is as follows: if you have a structure purchased (I.E. Auto Clicker which is +1 cookie every 2 seconds) it does not add a cookie to your total. Instead it adds a 1 to the end of your cookie total. For example, if you have 1004 cookies, instead of bringing you to 1005 cookies, it is making your total 10041 cookies. And this just keeps going and going. The same thing happens to the cost of structures. For example, (Note my formula for increasing cost is: cost += cost*.3) if you purchase your first auto clicker for 50 cookies, the next auto clicker cost 5015 cookies instead of 65 cookies. I have no idea why this is happening. Any explanations are appreciated.
<!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>
Thanks, Michael
Upvotes: 1
Views: 118
Reputation: 31
add the Number(); tag aroundwhere you get the bigger number (1004)
Number(<code for getting number>) + <other number> = variable
Upvotes: 0
Reputation: 6081
try this AutoClickCookie()
instead of your AutoClickCookie()
function :
function AutoClickCookie() {
cookieClicks = parseInt(cookieClicks)+parseInt((AutoClickers*1),10);
}
Upvotes: 0
Reputation: 445
I didn't sift through all of your code, but after reading your description, I imagine that your problem is that you are not converting the string data to a number. Remember that localStorage only stores string values. So, when you need a number, I suggest you use the built-in parseInt function.
var cost = parseInt(localStorage.AutoClickerCost, 10);
// 10 means base 10 here (optional, but helpful)
Upvotes: 2