尽管使用了off,click函数仍会触发,这可能也会导致数组问题

click function firing despite using off, probably causing the array issue too

本文关键字:问题 数组 off click 函数      更新时间:2024-06-07

我正在开发一款西蒙游戏,我觉得我已经接近尾声了(耶!),不过我似乎遇到了一个问题。在玩家匹配西蒙的第一个移动后,它将移动到下一轮,并用第二个移动填充阵列。然后,您可以匹配第二个移动,它也可以正确地迭代。但我认为它是第二次发射,然后通常是错误的,然后使玩家阵列将一步作为最后一步,而不是重置。

如何确保按钮只触发一次,以及如何正确重置玩家阵列?

function playerInput(simon, player, j){
    while (j < simon.length)  
        {
            console.log(j);
            if(simon[j] === player[j])
            {
                console.log("player pressed the correct button");
                j++;
                // potentially not working off function
                $(".simon-button").off().on('click', function() {
                    // stops multiple executions
                });
                buttons(simon, j, player);
            }
            else
            {
                if(strict === true)
                {
                    reset();
                    return; 
                }
                else
                {
                    console.log("player pressed the wrong button");
                    // seems to be screwing up here when it's wrong
                    // but not when used to reset player when it's right
                    player = [];
                    repeatMoves(player);
                    return;
                }
            }
        }
    if(j >= simon.length && simon.length < 20) 
        {
            j = 0;
            player = [];
            newMove(player);
        }
    else
    {
        alert("You win!!!");
        successSound.play();
        reset();
    }   
}
function buttons(simon, j, player){
    green.css('cursor', 'pointer');
    red.css('cursor', 'pointer');
    blue.css('cursor', 'pointer');
    yellow.css('cursor', 'pointer');
    $(".simon-button").click(function(){
        if($(this).hasClass("green-button"))
            {
                player.push(greenPushed());
                playerInput(simon, player, j);
            }
        else if($(this).hasClass("red-button"))
            {
                player.push(redPushed());
                playerInput(simon, player, j);
            }
        else if($(this).hasClass("blue-button"))
            {
                player.push(bluePushed());
                playerInput(simon, player, j);
            }
        else if($(this).hasClass("yellow-button"))
            {
                player.push(yellowPushed());
                playerInput(simon, player, j);
            }
        console.log(player);
        console.log(simon);
    });
}

https://jsfiddle.net/uoyg2jx9/1/

我忘了提一下,为了让小提琴工作,点击空按钮,然后点击启动按钮。

我还没有充分消化你的代码,无法理解它应该做什么,但它确实有一点无休止的循环:

while (j < simon.length)  {
    if(simon[j] === player[j]) {
        // ...
        j++; // ok
        // ...
    } else {
        // nothing in this clause modifies j.  To infinity and beyond...
    }
}

这里有一个i=0;,之前没有提到i,所以也许这就是问题所在(尽管将j设置为零仍然会让你陷入无尽的循环。)我怀疑你想在else中打破while循环,也许是通过设置j == simon.length,或者在repeatMoves()之后设置returning。