JavaScript将字符串添加到数字而不是值

JavaScript adding string to number instead of value

本文关键字:数字 字符串 添加 JavaScript      更新时间:2024-02-25

我最近在保存游戏时遇到了一个问题,经过进一步的处理,我成功地获得了localStorage();去工作!不幸的是,当你刷新或重新打开游戏时,会出现一个严重的错误。我尝试重新创建的游戏是Cookie Clicker。错误如下:如果你购买了一个结构(即每2秒+1个cookie的自动点击器),它不会在你的总数中添加cookie。相反,它会在饼干总数的末尾加上1。例如,如果你有1004个cookie,而不是让你有1005个cookie,这意味着你总共有10041个cookie。这种情况一直持续下去。同样的事情也发生在建筑成本上。例如,(请注意,我增加成本的公式是:cost+=cost*.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>

谢谢,Michael

我没有筛选您的所有代码,但在阅读了您的描述后,我认为您的问题是没有将字符串数据转换为数字。请记住,localStorage只存储字符串值。所以,当你需要一个数字时,我建议你使用内置的parseInt函数。

var cost = parseInt(localStorage.AutoClickerCost, 10); 
// 10 means base 10 here (optional, but helpful)

尝试此AutoClickCookie()而不是AutoClickCookie()函数:

function AutoClickCookie() {
                cookieClicks = parseInt(cookieClicks)+parseInt((AutoClickers*1),10);
                }

添加数字() 在这里你可以得到更大的数字(1004)

Number(<code for getting number>) + <other number> = variable