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

标签 javascript html add

我最近在保存我的游戏时遇到了问题,在进一步摆弄之后,我成功地获得了 localStorage();上类!不幸的是,当您刷新或重新打开游戏时,会出现一个严重的错误。我正在尝试重新创建的游戏是 Cookie Clicker。错误如下:如果您购买了一个结构(即 Auto Clicker,每 2 秒 +1 个 cookie),它不会将 cookie 添加到您的总数中。相反,它会将 1 添加到您的 cookie 总数的末尾。例如,如果您有 1004 个 cookies ,它不会让您得到 1005 个 cookies ,而是使您的 cookies 总数达到 10041 个。这只会一直持续下去。同样的事情也发生在结构的成本上。例如,(请注意我增加成本的公式是:成本 += 成本*.3)如果您购买第一个自动答题器需要 50 个 cookies ,下一个自动答题器将花费 5015 个 cookies 而不是 65 个 cookies 。我不知道为什么会这样。任何解释表示赞赏。

<!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>

谢谢, 迈克尔

最佳答案

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

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

关于JavaScript 将字符串添加到数字而不是值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28654337/

相关文章:

javascript - 使用 JS window.print() 中断 chrome 中的后退按钮

python - Tensorflow 中的基本添加?

c++ - 排序链表和addSorted函数问题。

javascript - 使用 PHP 的 jQuery Ajax POST 示例

php - 如何在javascript文件中添加PHP标签

javascript - JQTOUCH,绑定(bind)到通过 AJAX 拉入的链接,进行另一个 AJAX 调用?可能的?

javascript - momentJS 日期字符串添加 5 天

javascript - Bokeh Callback 是否更新了列表而不是 ColumnDataSource?

html - 为什么会在代码中引用常规和缩小的 Bootstrap CSS?

html - 纯 CSS onclick 事件复选框 hack 问题