gpt4 book ai didi

javascript - 我的 javascript setinterval 循环乘以 2 而不是加 1

转载 作者:行者123 更新时间:2023-11-30 06:51:09 24 4
gpt4 key购买 nike

我正在尝试构建一个游戏循环,它可以很好地处理两个循环,但是当我尝试添加第三个循环时,它只是乘以二(我的 "fires" var)添加“1”。你能告诉我哪里可能有问题吗?

function buyFire(){
var fireCost = Math.floor(10 * Math.pow(1.1,fires)); //works out the cost of this cursor
if(cookies >= fireCost){ //checks that the player can afford the cursor
fires = fires + 1; //increases number of cursors
cookies = cookies - fireCost; //removes the cookies spent
document.getElementById('fires').innerHTML = fires; //updates the number of cursors for the user
document.getElementById('cookies').innerHTML = cookies;


};
var nextCost = Math.floor(10 * Math.pow(1.1,fires)); //works out the cost of the next cursor
document.getElementById('fireCost').innerHTML = nextCost; //updates the cursor cost for the user
};


window.setInterval(function() {
cookieClick(caves);
}, 900);

window.setInterval(function() {
cookieClick(cursors);
}, 1000);

setInterval(function() {
fireClick(fires);
}, 1200);

完整代码:https://pastebin.com/4rJGWVFG

最佳答案

在这种情况下,你给火是 1 并加上一个数字。

var fires = 1;

function fireClick(number){
fires = fires + number;
console.log(fires);
};

setInterval(function(){ fireClick(fires); }, 1200);

所以这就是你想要做的:

var fires = 1;

function fireClick(number){
fires += number;
console.log(fires);
};

setInterval(function(){ fireClick(1); }, 1200);

而不是像这样添加:fires = fires + number;
你可以这样写:fires += number;

问题是 setInterval() 函数。您有重复的 setIntervals:

var cookies = 0;
var fires = 0;
var cursors = 0;
var caves = 0;

document.getElementById("1").style.visibility = "hidden";
document.getElementById("1.1").style.visibility = "hidden";
document.getElementById("2").style.visibility = "hidden";
document.getElementById("3").style.visibility = "hidden";

function cookieClick(number) {
cookies = cookies + number;
document.getElementById("cookies").innerHTML = cookies;
};

function fireClick(number) {
fires += number;
document.getElementById("fires").innerHTML = fires;
};

function buyCursor() {
//works out the cost of this cursor
var cursorCost = Math.floor(10 * Math.pow(1.1, cursors));
//checks that the player can afford the cursor
if (cookies >= cursorCost) {
//increases number of cursors
cursors = cursors + 1;
//removes the cookies spent
cookies = cookies - cursorCost;
//updates the number of cursors for the user
document.getElementById('cursors').innerHTML = cursors;
//updates the number of cookies for the user
document.getElementById('cookies').innerHTML = cookies;
document.getElementById("1").style.visibility = "visible";
};
//works out the cost of the next cursor
var nextCost = Math.floor(10 * Math.pow(1.1, cursors));
//updates the cursor cost for the user
document.getElementById('cursorCost').innerHTML = nextCost;
};

function buyCave() {
//works out the cost of this cursor
var caveCost = Math.floor(10 * Math.pow(1.1, caves));
//checks that the player can afford the cursor
if (cookies >= caveCost) {
//increases number of cursors
caves = caves + 1;
//removes the cookies spent
cookies = cookies - caveCost;
//updates the number of cursors for the user
document.getElementById('caves').innerHTML = caves;
document.getElementById('cookies').innerHTML = cookies;
document.getElementById("1.1").style.visibility = "visible";
//makes update 1 visible
document.getElementById("2").style.visibility = "visible";
};
//works out the cost of the next cursor
var nextCost = Math.floor(10 * Math.pow(1.1, caves));
//updates the cursor cost for the user
document.getElementById('caveCost').innerHTML = nextCost;
};

function fireUp() {
if (cookies >= 1) {
document.getElementById("3").style.visibility = "visible";
};
};

function buyFire() {
//works out the cost of this cursor
var fireCost = Math.floor(10 * Math.pow(1.1, fires));
//checks that the player can afford the cursor
if (cookies >= fireCost) {
//increases number of cursors
fires = fires + 1;
//removes the cookies spent
cookies = cookies - fireCost;
//updates the number of cursors for the user
document.getElementById('fires').innerHTML = fires;
document.getElementById('cookies').innerHTML = cookies;
};
//works out the cost of the next cursor
var nextCost = Math.floor(10 * Math.pow(1.1, fires));
//updates the cursor cost for the user
document.getElementById('fireCost').innerHTML = nextCost;
};

window.setInterval(function() {
cookieClick(caves);
}, 900);

window.setInterval(function() {
cookieClick(cursors);
}, 1000);

window.setInterval(function() {
fireClick(1);
}, 1200);
<div class=col-xs-3>       
<span id="2">
<center>
Технологии
<br />
<button onclick="fireUp(1)">Приручение огня</button>
<br />
</center>
</span>
</div>

<div class=col-xs-6>
<center>
<button onclick="cookieClick(1)">Человек</button>
<br /> Население: <span id="cookies">0</span>
<br /> <button onclick="buyCursor()">Buy Cursor</button>
<br /> Cursors: <span id="cursors">0</span>
<br /> Cursor Cost: <span id="cursorCost">10</span>
<br />
<span id="1">
<button onclick="buyCave()">Найти пещеру</button>
<br /> Caves: <span id="caves">0</span>
<br /> Cave Cost: <span id="caveCost">10</span>
</span>
<br />
<span id="1.1">Люди замерзают, необходимы костры</span>
</center>
</div>

<div class=col-xs-3>
<span id="3">
Постройки
<br /><button onclick="fireClick(1)">Костер</button>
<br />Костров: <span id="fires">0</span>
<br /><button onclick="buyFire()">Разводитель костров</button>
<br />
<span id="fireCost">10</span>
</span>
</div>

关于javascript - 我的 javascript setinterval 循环乘以 2 而不是加 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47318693/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com