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

时间:2017-11-15 22:52:27

标签: javascript game-loop

我正在尝试建立一个游戏循环,它可以正常工作两个循环,但是当我尝试添加第三个循环时,它只是乘以2(我的“激发” 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

2 个答案:

答案 0 :(得分:0)

你给火是1,并在这种情况下用数字加上它自己发射。

000000000000000000

所以这就是你想要做的事情:

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;功能。你有重复的setIntervals:

setInterval()
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);

答案 1 :(得分:-1)

这可能已在评论中得到解答,但您在完整来源中指定了fireClick(),如下所示:

    $day = $workDays[$weekIndex][($col - 1) % 7] == TRUE ? '<SPAN class="' . $class . '"onclick="Materialize.toast(\'<?= "Hello World"\')" >' . $dkey  .'</SPAN>' : $dkey; // Defines SPAN to style related days, otherwise defaults

你可能打算把它称为fireClick(1),而不是fireClick(fires),它返回fires * 2.