2017-06-18 57 views
0

我正在切換布爾變量「userTurn」。有兩個函數集「userTurn」的價值:切換布爾變量在單擊事件條件語句中評估

  1. runGame(),在本輪的最後一個聲音播放完畢後這臺「userTurn」爲真。

  2. buttonClick(),只應在「userTurn」爲true時執行。 buttonClick在用戶成功複製當前模式或用戶犯了錯誤之後將「userTurn」設置爲false。

    我正在評估單擊事件內部的條件語句中「userTurn」的值。

    $(".quarter").on('click', function(){ 
        if(userTurn===true && isOn===true){ 
         var color = $(this).attr('id'); 
         clearTimeout(buzz); 
         buttonClick(color); 
        } 
    }) 
    

    用戶成功複製當前模式後,或者如果用戶犯了錯誤,「userTurn」設置爲false。我遇到的問題是,在「userTurn」設置爲false之後,條件語句內的代碼仍在執行。目標是「.quarter」僅在「userTurn」爲true時纔可點擊。爲什麼buttonClick()函數在「.quarter」被點擊時仍然執行,即使當「userTurn」爲false?

以下是設置 「userTurn」 的值的兩個功能:)

  1. runGame(:)

    function runGame(){ 
        if(isOn===true){ 
         userTurn = false; 
         count(); 
         playPattern(order, index); 
         if(index===counter-1&&userTurn===false){ 
          userTurn=true; 
          clearInterval(play); 
          buzz = setTimeout(buzzer, 10000); 
         }else{ 
          index++; 
         } 
        } else { 
         clearInterval(play); 
         clearTimeout(buzz); 
        } 
    } 
    

    2.buttonClick(:

    function buttonClick(color){ 
        var yellowSound = document.getElementById("horseSound"); 
        var redSound = document.getElementById("endFx"); 
        var greenSound = document.getElementById("westernRicochet"); 
        var blueSound = document.getElementById("robotBlip"); 
        $("#red").mousedown(function(){ 
         $("#red").css("background-color", "#ff4d4d"); 
         redSound.play(); 
        }); 
        $("#red").mouseup(function(){ 
         $("#red").css("background-color", "#ff0000"); 
         redSound.pause(); 
        }); 
        $("#blue").mousedown(function(){ 
         $("#blue").css("background-color", "#0000e6"); 
         blueSound.play(); 
        }); 
        $("#blue").mouseup(function(){ 
         $("#blue").css("background-color", "#000099"); 
         blueSound.pause(); 
        }); 
        $("#green").mousedown(function(){ 
         $("#green").css("background-color", "#00e600"); 
         greenSound.play(); 
        }); 
        $("#green").mouseup(function(){ 
         $("#green").css("background-color", "#009900"); 
         greenSound.pause(); 
        }); 
        $("#yellow").mousedown(function(){ 
         $("#yellow").css("background-color", "#ffff4d"); 
         yellowSound.play(); 
        }); 
        $("#yellow").mouseup(function(){ 
         $("#yellow").css("background-color", "#ffff00"); 
         yellowSound.pause(); 
        }); 
        if(color===order[compareIndex]){ 
         userPattern.push(color); 
         console.log(userPattern, "match"); 
         buzz = setTimeout(buzzer, 10000); 
         compareIndex++; 
         if(userPattern.length===counter){ 
          userTurn=false; 
          compareIndex=0; 
          index=0; 
          counter++; 
          count(); 
          userPattern.length=0; 
          play = setInterval(runGame, 2000); 
          clearTimeout(buzz); 
         } 
        } else { 
         userTurn=false; 
         console.log('don\'t match'); 
         clearTimeout(buzz); 
         index=0; 
         compareIndex = 0; 
         userPattern.length=0; 
         buzz = setTimeout(buzzer, 1); 
        } 
    } 
    

在進入CODEPEN之前關閉你的容量! 這裏是codepen的鏈接。 要開始遊戲,請點擊「開啓」,然後點擊「開始」。時間很慢,所以您必須等待模式播放,然後按照與播放順序相同的順序單擊按鈕。問題出現在第一個聲音播放後。從那時起,只要用戶點擊,就會播放聲音並點亮按鈕,即使遊戲正在播放該模式並且「userTurn」爲false。另外值得警告的是,這仍然是一項正在進行中的工作,還有一些我正在研究的其他錯誤,例如用戶所做的第一次轉動不會點亮或播放聲音,但選擇會被推入正確的陣列和遊戲將根據您的選擇正確進行。我知道這是很多信息,但我堅持這一點,所以任何反饋將非常感激。謝謝!

回答

0

您使用buttonClick來處理點擊按鈕,但裏面buttonClick你設置mouseupmousedown每個按鈕事件偵聽器爲好。由於各個按鈕都有自己的mouseupmousedown聽衆,因此無論是否再次調用buttonClick,都會發生這些事件。

您將不得不檢查每個事件處理程序中的userTurn是否爲true以防止它們觸發。另外,最好在buttonClick之外設置這些,這樣您就不會在每次調用buttonClick時添加新的聽衆。