简体   繁体   English

西蒙说游戏的按钮响应问题

[英]Button response issues for a Simon Says game

Ive been developing a Simon says game recently that adds to the score if you click the correct button which there are 3, 1) green 1 2) red 2 3) trick我最近一直在开发一个西蒙说的游戏,如果你点击正确的按钮,它会增加分数,其中有 3, 1) 绿色 1 2) 红色 2 3) 技巧

I've noticed that when I run the game and click the appropriate buttons only one will add to the score whilst the other two subtract from it (Regardless of what the statement says).我注意到当我运行游戏并单击适当的按钮时,只有一个会增加分数,而另外两个会从中减去(无论声明是什么)。 Im unsure why this is the case and was wondering if anyone had any insights).我不确定为什么会这样,想知道是否有人有任何见解)。 My thought is that the if functions don't seem to correlate to the new statement that is generated.我的想法是 if 函数似乎与生成的新语句无关。

Any suggestions are appreciated,任何建议表示赞赏,

 var answers = [ "Simon says click red !", "Simon says click green !", "Simon says click 1 !", "Simon says click 2 !", "Click green", "Click red", "Click 1", "Click 2!" ]; var score = 0; var total = document.getElementById("score"); var statement = document.getElementById("instruct"); var randomStatement = answers[Math.floor(Math.random() * answers.length)]; function refresh(){ var randomStatement = answers[Math.floor(Math.random() * answers.length)]; statement.innerHTML = randomStatement; } function pressTrick(){ if(randomStatement === "Click green" || randomStatement === "Click red" || randomStatement === "Click 1" || randomStatement === "Click2!"){ score++; total.style.color = "green"; total.innerHTML = "Total score: " + score; setTimeout(refresh,900); } else { total.style.color = "red"; score--; total.innerHTML = "Total score: " + score; setTimeout(refresh,900); } } function pressRed(){ if(randomStatement === "Simon says click red !" || randomStatement === "Simon says click 2 !"){ score++; total.style.color = "green"; total.innerHTML = "Total score: " + score; setTimeout(refresh,900); } else { total.style.color = "red"; score--; total.innerHTML = "Total score: " + score; setTimeout(refresh,900); }} function pressGreen(){ if(randomStatement === "Simon says click 1 !" || randomStatement === "Simon says click green !"){ score++; total.style.color = "green"; total.innerHTML = "Total score: " + score; setTimeout(refresh,900); } else { total.style.color = "red"; score--; total.innerHTML = "Total score: " + score; setTimeout(refresh,900); }} function start(){ var i = 60; var count = setInterval(timer,1000); refresh(); function timer() { var display = document.getElementById("timer"); var finished = document.getElementById("heading"); if(i < 1){ clearInterval(count); finished.innerHTML = "Game Over! Your final Score is : " + score; display.style.color = "red"; display.style.fontWeight = "bold"; document.body.style.backgroundColor = "black"; } else if(i <= 10) { i--; display.style.color = " red"; display.style.fontWeight = "bold"; display.innerHTML = i + " Seconds Left"; } else { i--; display.style.color = "green"; display.style.fontWeight = "bold"; display.innerHTML = i + " Seconds Left"; }}}
 <html> <head> <title> Simon Says! </title> <link rel = "stylesheet" href = "simonsays.css"/> </head> <body> </br> <h1> Test Your Reflexes! </h1> <div id = "heading"; class = "h2"> Click on Simon to Begin! </div> </br> <img src = "boy.jpg" onclick = "start()"; onclick = "timer()"; onclick = "returnStatement";/> </br> <div id = "instruct" class="statement"></div> </br> <div class = "align"> <input type = "button" class = "button2" id = "button2" value = "1" onclick = "pressGreen()"; ></input> <input type = "button" class = "button" id = "button" value = "2" onclick = "pressRed()"; ></input> <input type = "button" class = "button3" id = "button3 " value = "Trick" onclick = "pressTrick()";></input> </div> </br> <div id = "score" class = "score"><b> Score: </b></div> <div id = "timer" class = "timer"><b> Time left: </b></div> <script src = "simonsays.js"></script> </body> </html>
Thank you! 谢谢!

There's just one word too much: var .只有一个词太多了: var In

function refresh(){
    var randomStatement = answers[Math.floor(Math.random() * answers.length)];
    statement.innerHTML = randomStatement;
}

the var causes a new, local variable randomStatement to be defined, and the global randomStatement remains unchanged, so all comparisons in the rest of the program use the initial rather than the refreshed value of randomStatement . var导致定义一个新的局部变量randomStatement ,并且全局randomStatement保持不变,因此程序其余部分中的所有比较都使用randomStatement的初始值而不是刷新值。 Drop the var here, and it works as expected.var放在此处,它会按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM