JavaScript 将字符串添加到数字而不是值
JavaScript adding string to number instead of value
我最近在保存游戏时遇到了问题,经过进一步的摆弄,我成功地获得了 localStorage();去工作!不幸的是,当您刷新或重新打开游戏时,会出现一个严重的错误。我正在尝试重新创建的游戏是 Cookie Clicker。错误如下:如果您购买了一个结构(即 Auto Clicker,每 2 秒 +1 个 cookie),它不会将 cookie 添加到您的总数中。相反,它会将 1 添加到您的 cookie 总数的末尾。例如,如果您有 1004 个饼干,它不会让您达到 1005 个饼干,而是使您的饼干总数达到 10041 个。这只会一直持续下去。同样的事情也发生在结构的成本上。例如,(请注意我增加成本的公式是:成本 += 成本*.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>
谢谢,
迈克尔
我没有仔细检查您的所有代码,但在阅读了您的描述后,我想您的问题是您没有将字符串数据转换为数字。请记住,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(); 标签
Number(<code for getting number>) + <other number> = variable
我最近在保存游戏时遇到了问题,经过进一步的摆弄,我成功地获得了 localStorage();去工作!不幸的是,当您刷新或重新打开游戏时,会出现一个严重的错误。我正在尝试重新创建的游戏是 Cookie Clicker。错误如下:如果您购买了一个结构(即 Auto Clicker,每 2 秒 +1 个 cookie),它不会将 cookie 添加到您的总数中。相反,它会将 1 添加到您的 cookie 总数的末尾。例如,如果您有 1004 个饼干,它不会让您达到 1005 个饼干,而是使您的饼干总数达到 10041 个。这只会一直持续下去。同样的事情也发生在结构的成本上。例如,(请注意我增加成本的公式是:成本 += 成本*.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>
谢谢, 迈克尔
我没有仔细检查您的所有代码,但在阅读了您的描述后,我想您的问题是您没有将字符串数据转换为数字。请记住,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(); 标签
Number(<code for getting number>) + <other number> = variable