Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

78
Visualizações
AddEventListener is working at the wrong time

For some reason, the addEventListener is working both when it should work, and when it shouldn't work. I made a game where if the player hasn't lost, they play, but only when they lose, they can click on the screen to retry. In the browser, when the game is opened, or the browser is refreshed, everything works fine. However, after the first loss, any clicks cause the game to restart from the beginning. What am I doing wrong?

function main() {
  setTimeout(function everyFrame() {
    clearCanvas();
    //Run if the game isn't over
    if (!isGameOver()) {
      game_over = false;

      //Code for the game screen
    }

    //Run if the game is over
    else if (isGameOver()) {
      game_over = true;

      //Code for the lose screen

      //Reset the game when the user clicks on the lose screen
      if (game_over) {
        game_over = false;
        myCanvas.addEventListener("click", restartGame);
      }
    }
    main();
    changingDir = false;
  }, 100);
}
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda