keylistener在第一次工作时不起作用

时间:2015-12-05 11:38:34

标签: javascript

我的程序就像一个反应游戏。按“向上箭头”时背景将颜色更改为红色。你需要在2秒内按下按钮。如果你不按,那么你将失去其他游戏。但是在最初的2秒钟你输了但是比赛进展顺利。那么前2秒代码的问题是什么?

我的代码:

var ido = 2000;
var nyomott = 0;
var nemelso = 0;
// >> main()
function main() {
  nyomott = 0;
  var r = Math.floor(Math.random() * 4);
  var szin;

  switch (r) {
    case 0:
      szin = "red";
      break;
    case 1:
      szin = "green";
      break;
    case 2:
      szin = "yellow";
      break;
    case 3:
      szin = "blue";
      break;
  }

  var print = "<h1>" + "Pess the key: " + "</br>" + szin + "</h1>" + "</br>";
  document.getElementById("results").innerHTML = print;

  startTimer();
}

var ciklus = setInterval(startTimer, ido);

// >> startTimer() : This function starts the timer
function startTimer() {
  timerId = setTimeout(function() {
    if (nyomott == 0) {
      document.getElementById("results").innerHTML = "<h1>Lose</h1>";
      clearInterval(ciklus);
    } else {
      main();
    }
  }, ido);

}

document.addEventListener("keydown", function(inEvent) {

  if (inEvent.keyCode == 38) {
    document.body.style.backgroundColor = "red";
    nyomott = 1;
    console.log(nyomott);
  } else if (inEvent.keyCode == 404) document.body.style.backgroundColor = "green";
  else if (inEvent.keyCode == 405) document.body.style.backgroundColor = "yellow";
  else if (inEvent.keyCode == 406) document.body.style.backgroundColor = "blue";
});
body {
  width: 100%;
  height: 100%;
  background-color: #202020;
}
div {
  position: absolute;
  height: 100%;
  width: 100%;
  display: table;
  font-size: 60px;
  color: #ffffff;
}
h1 {
  display: table-cell;
  vertical-align: middle;
  text-align: center;
  color: #FFFFFF;
}
<div onload="main()" id="results">
  <h1></h1>
</div>

1 个答案:

答案 0 :(得分:1)

问题是,在function main()中,您设置的nyomott = 0;符合function startTimer()中打印'丢失'的条件。

做了一些调整

//pushed your variables up to be above the event listener function to be able to set nyomotto = 1

var ido = 2000;
var nyomott = 0;
var nemelso = 0;
document.addEventListener("keydown", function(inEvent) {

  if (inEvent.keyCode == 38) {
    document.body.style.backgroundColor = "red";
    nyomott = 1;
    console.log(nyomott);
  } else if (inEvent.keyCode == 404)
    document.body.style.backgroundColor = "green";
  else if (inEvent.keyCode == 405)
    document.body.style.backgroundColor = "yellow";
  else if (inEvent.keyCode == 406)
    document.body.style.backgroundColor = "blue";
});

function main() {
  var r = Math.floor(Math.random() * 4);
  var szin;

  switch (r) {
    case 0:
      szin = "red";
      break;
    case 1:
      szin = "green";
      break;
    case 2:
      szin = "yellow";
      break;
    case 3:
      szin = "blue";
      break;
  }

  var print = "<h1>" + "Pess the key: " + "</br>" + szin + "</h1>" + "</br>";
  document.getElementById("results").innerHTML = print;

  startTimer();
}
var ciklus = setInterval(startTimer, ido);

// This function starts the timer
function startTimer() {
  timerId = setTimeout(function() {
    if (nyomott == 0) {
      document.getElementById("results").innerHTML = "<h1>Lose</h1>";
      clearInterval(ciklus);
    } else {
      main();
    }
  }, ido);

}
    body {
      width: 100%;
      height: 100%;
      background-color: #202020;
    }

    div {
      position: absolute;
      height: 100%;
      width: 100%;
      display: table;
      font-size: 60px;
      color: #ffffff;
    }

    h1 {
      display: table-cell;
      vertical-align: middle;
      text-align: center;
      color: #FFFFFF;
    }
<div onload="main()" id="results">
  <h1></h1>
</div>