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

115
Visualizações
How to make a menu screen with if statements

I am trying to make three difficulty settings for a "game" I am using to experiment & learn JavaScript.

I have tried by setting a variable, and three key inputs which value it 1, 2, or 3. By tying each "difficulty" setting to an if statement conditional upon one value of the aforementioned variable. The keys do successfully set the variable, but they do not impact the game in the manner I hoped.

However, if after setting the variable to a value I replace "var gameMode = 0" with "var gameMode", it will open that difficulty level. How might I go about rectifying this?

Here is what I have so far done in the menu:

var gameMode = 0;

if (gameMode === 0) {
  draw = function() {
    background(255, 255, 255);
    fill(0, 0, 0);
    text("Mover Game", 160, 200);
    text(gameMode, 160, 220);

    keyPressed = function() {
      if (keyCode === 112) {
        gameMode = 1;
      }
      if (keyCode === 113) {
        gameMode = 2;
      }
      if (keyCode === 114) {
        gameMode = 3;
      }
    };
  };
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Your code is very limited but when looking at the code,

You are setting var gameMode = 0; at the beginning and inside the keyPressed function you are just checking if the appropriate key is pressed and just assigning the value to gameMode. You have to run the game again with the newly assigned game mode.

My suggestion is to use two functions. One to run the game and other to deal with the key press and initiate the game.

function runMyGame(gameMode = 0){
   // defaults the gameMode to 0 so it will start the game in 0 mode initially

  // here comes your game codes
}

// adding keypress listener
document.addEventListener("keypress", myKeyPressFunction); 

// function which fires on key press
function myKeyPressFunction(){

   if (keyCode === 112) {
       gameMode = 1;
       runMyGame(gameMode);
   }
   if (keyCode === 113) {
       gameMode = 2;
       runMyGame(gameMode);
   }
   if (keyCode === 114) {
       gameMode = 3;
       runMyGame(gameMode);
   }
}
about 4 years ago · Juan Pablo Isaza Relatório
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