1

我正在尝试编写倒数计时器函数来检查是否满足游戏的成功条件。如果是,则该函数执行成功函数,如果不是,则继续倒计时直到 0。

我的问题是我的 if 语句与我的游戏变量(nimbus_char_count并且nimbus_line_count似乎从未评估过错误。我不知道为什么。有什么建议吗?谢谢。

var nimbus_char_count = 3;
var nimbus_line_count = 4;
var i = 2;
var j = 2;

function set_char(increment) {
    //Incrementally adds the nimbus_drop class to successive characters
    if (increment === 1 && nimbus_char_count < 3) {
        nimbus_char_count = nimbus_char_count + 1;
        $("#nimbus_character_count").html(nimbus_char_count);
        $("#nimbus_char" + (nimbus_char_count - 1)).addClass("nimbus_drop");
    } else if (increment === -1 && nimbus_char_count > 0) {
        nimbus_char_count = nimbus_char_count - 1;
        $("#nimbus_character_count").html(nimbus_char_count);
        $("#nimbus_char" + nimbus_char_count).removeClass("nimbus_drop");
    } else {
        nimbus_flash_red();
    }
}

function set_lines(increment) {
    //Incrementally adds the nimbus_drop class to all characters
    if ((increment === 1 && nimbus_line_count < 4) || (increment === -1 && nimbus_line_count > 1)) {
        //First remove the old class
        for (i = 2; i >= 0; i = i - 1) {
            $("#nimbus_char" + i).removeClass("nimbus_drop" + nimbus_line_count);
        }
        //Then add it back in
        nimbus_line_count = nimbus_line_count + increment;
        $("#nimbus_line_count").html(nimbus_line_count);
        for (j = 2; j >= 0; j = j - 1) {
            $("#nimbus_char" + j).addClass("nimbus_drop" + nimbus_line_count);
        }
    } else {
        nimbus_flash_red();
    }
}

function countdown(count, container) { //Countdown function for the game
    var $container = $(container);
    var count_init = count;

    (function step() {
        $("#timer_cover").width(((1 - (count / count_init)) * 100) + '%'); //Set the width of the timer bar
        count = count - 20; //Decrements by 20ms every iteration

        if ($("nimbus_char_count") !== '1' || $("nimbus_line_count") !== '2') { //Tests success conditions…
            if (count > -20) {
                setTimeout(step, 20); //…and waits 20ms if they weren't met and there still is time
            } else {
                nimbus_failure(); //…and fails the player if they aren't met and there is no time
            }
        } else {
            nimbus_success(); // …and calls the success function if the conditions are met
        }
    })();
}

$(document).ready( function () {
    countdown(7000, '#timer_cover'); //Initiates the countdown
});
4

0 回答 0