简体   繁体   English

javascript停止键不再被使用

[英]javascript stop key from being used again

My goals are to stop printing out the key if it's been used before, and to stop the game when all the letters are displayed (game won).我的目标是停止打印之前使用过的密钥,并在显示所有字母时停止游戏(游戏获胜)。

SOLVED: For those who have the same problem: Adding this function will do the job:已解决:对于那些有同样问题的人:添加此功能将完成这项工作:

 //check if key is used function checkKey(charStr) { if (keyStore.includes(charStr)) { return false; } else { //record pressed key keyStore.push(charStr); if (nameChosen.toLowerCase().includes(charStr)) { display(charStr); } else { attempt--; //print out wrong-guess key document.getElementById("wrongGuess").innerHTML = document.getElementById("wrongGuess").innerHTML + charStr + " "; } } }

and put the function inside the keypress event.并将函数放在按键事件中。

Here's my old code:这是我的代码:

var names = ["Adam Red", "Bob Green", "Chris Blue"];
var nameChosen = names[Math.floor(Math.random() * names.length)];
var nameSplit = nameChosen.split("");
var updated = [];
var attempt = nameChosen.length - 2;

function display(char) {
    //replace each of the letters with underscore and space with dash
    char = char || "_"; // set to default when no char given
    var nameDisplayed = [];

    for (var i = 0; i < nameSplit.length; i++) {
        if (updated[i] || (char.toLowerCase() === nameSplit[i].toLowerCase())) {
            nameDisplayed += nameSplit[i] + " ";
            updated[i] = true;
        }
        else if (nameSplit[i] == " ") {
            nameDisplayed += "- ";
        } 
        else {
            nameDisplayed += "_ ";
        }
    }

    document.getElementById("nameDisplay").innerHTML = nameDisplayed;
}

// need function for game won <===============

function gameLost() {
    document.getElementById("container").style.backgroundColor = "red";
    document.getElementById("notice").innerHTML = "SORRY, YOU'VE LOST THE GAME!";
    document.getElementById("notice").style.fontWeight = "bold";
    document.getElementById("notice").style.color = "white";
}

document.getElementById("attempt").innerHTML = attempt;

//get key pressed by user
document.addEventListener("keypress", function(evt) {
    evt = evt || window.event;
    var charCode = evt.keyCode || evt.which;
    var charStr = String.fromCharCode(charCode);

    console.log("key pressed: ", charStr);

    if (nameChosen.toLowerCase().includes(charStr)) {
        display(charStr);
    }
    else {
        attempt--;
        document.getElementById("wrongGuess").innerHTML = document.getElementById("wrongGuess").innerHTML + charStr + " ";
         // print out key pressed continuously <==================
    };

    if (attempt == 0) {
        gameLost();
    };

    document.getElementById("attempt").innerHTML = attempt;
});

display(); // call onload

I've tried: add new var and set it true/false and put in the loop, create a new function but still can't make it happen.我试过:添加新变量并将其设置为真/假并放入循环中,创建一个新函数但仍然无法实现。

It looks like you're building a game of hang man?看起来你正在制作一个吊人游戏?

Try searching through the nameDisplayed variable and see if any of its characters match '_'尝试搜索 nameDisplayed 变量并查看是否有任何字符与“_”匹配

if they don't, you've revealed all the characters and won the game!如果他们不这样做,您就已经展示了所有角色并赢得了比赛! if it does still have a '_' character, there must be another character left to find.如果它仍然有一个 '_' 字符,则必须有另一个字符可以找到。

Keeping a global array of keys pressed, and searching through that once a new key is pressed can help with the repetitive keys.保持按下的全局键数组,并在按下新键后搜索这些键可以帮助处理重复键。

As for stopping key input when you win, do some research into the removeEventListener function, which you can remove the key stroke even listener with.至于当你赢了就停止按键输入,研究一下 removeEventListener 函数,你可以用它来删除击键甚至监听器。

Hope that helped!希望有所帮助!

Solution to close the question:关闭问题的解决方案:

//check if key is used
function checkKey(charStr) {  
  if (keyStore.includes(charStr)) {
      return false;
  }
  else {
      //record pressed key
      keyStore.push(charStr); 

      if (nameChosen.toLowerCase().includes(charStr)) {
          display(charStr);
      }
      else {
          attempt--;

          //print out wrong-guess key
          document.getElementById("wrongGuess").innerHTML = document.getElementById("wrongGuess").innerHTML + charStr + " ";
      }
  }
}

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

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