简体   繁体   English

如何将 removeEventListener 添加到点击事件?

[英]How do I add a removeEventListener to a click event?

I searched online on how to disable an event, but everything was so confusing.我在网上搜索了如何禁用事件,但一切都很混乱。 I wonder if anyone could explain to me how I can disable a click event?我想知道是否有人可以向我解释如何禁用点击事件? Please comment if you have any questions.如果您有任何问题,请发表评论。 Thank you in advance, Someone told me to use a function.提前谢谢你,有人告诉我使用 function。 but I don't understand.但我不明白。

//here are the variables that I used in this section of code so you can understand it better
var monsters = ["slug", "zombie", "wasp"];
var randomForMon = Math.floor(Math.random() * monsters.length);
var randMonster = monsters[randomForMon];
var user_answer = getNumber("answer_input");

//I don't need to put anything in this function (do I?), I just want to disable a button if the user tries to click it.
function handleClick(event) {
  //what would I even put here?
}

//when monster clicked, show fighting symbol and change screen
onEvent(randMonster, "click", function( ){
  hideElement("clickMonster_label");
  showSymbol();
  setTimeout(function(){
    setScreen("equation_screen");
  }, 1300);
  newEquation();
  if ("answer_input" !== "user_answer") {
    //I would add removeEventListener here so that player cannot click monster if their answer is incorrect.
  }
});

Just letting you know, I don't know much about JavaScript, so maybe use simpler terms xD.只是让你知道,我对 JavaScript 了解不多,所以也许使用更简单的术语 xD。 I would really appreciate it, Again, if you have any questions.如果您有任何问题,我将再次感谢您。 please ask.请问。

Looks like you need to track answers for each monster, and if the are incorrect, do not allow the user to click the monster again.看起来你需要跟踪每个怪物的答案,如果不正确,不要让用户再次点击怪物。 To do this with your current architecture, below I setup an array of answers initially set to null, meaning no answer has been given.要使用您当前的架构执行此操作,我在下面设置了一组最初设置为 null 的答案,这意味着没有给出答案。 Within the click event, we will set the monsterAnswer to be true or false based on the user's response.在 click 事件中,我们将根据用户的响应将 monsterAnswer 设置为 true 或 false。 Hope this helps and welcome!希望这会有所帮助和欢迎!

 var monsters = ["slug", "zombie", "wasp"]; var randomForMon = Math.floor(Math.random() * monsters.length); var randMonster = monsters[randomForMon]; var user_answer = getNumber("answer_input"); var monstersAnswers = [null, null, null]; //when monster clicked, show fighting symbol and change screen onEvent(randMonster, "click", function() { const monsterAnswer = monstersAnswers[randomForMon]; if (monsterAnswer === false) { // don't do anything return; } hideElement("clickMonster_label"); showSymbol(); setTimeout(function() { setScreen("equation_screen"); }, 1300); newEquation(); var userAnswer = /* something */ monstersAnswers[randomForMon] = correctAnswer === userAnswer; });

Maybe you can just return in the click listener when the "answer_input".== "user_answer".当“answer_input”.==“user_answer”时,也许你可以在点击监听器中返回。

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

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