Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

119
Vistas
Trying to control movement in a game with arrow keys and and buttons with JS

This is a snake game but I'm trying to let users control the snake with arrow keys and clickable buttons. The arrow keys work fine but i can't get all the buttons to work, one button will work and then nothing else 😢

I've tried to put them in all in their own switch statement and then I tried an if-else but still Nada.

Any help or even just some info on what way I should go about it would be much appreciated.

let inputDirection = {
  x: 0,
  y: 0
}
let lastInputDirection = {
  x: 0,
  y: 0
}

let btnUp = document.querySelector(".btn-up")
let btnLeft = document.querySelector(".btn-left")
let btnRight = document.querySelector(".btn-right")
let btnDown = document.querySelector(".btn-down")


window.addEventListener('keydown', e => {

  switch (e.key) {

    case 'ArrowUp':
      // case btnUp:  
      console.log('hi');
      if (lastInputDirection.y !== 0) break
      inputDirection = {
        x: 0,
        y: -1
      }
      break

    case 'ArrowDown':
      if (lastInputDirection.y !== 0) break
      inputDirection = {
        x: 0,
        y: 1
      }
      break

    case 'ArrowLeft':
      if (lastInputDirection.x !== 0) break
      inputDirection = {
        x: -1,
        y: 0
      }
      break

    case 'ArrowRight':
      if (lastInputDirection.x !== 0) break
      inputDirection = {
        x: 1,
        y: 0
      }
      break

  }
})

function getInputDirection() {
  lastInputDirection = inputDirection
  return inputDirection
}
<button type="button" class="btn-up">Up</button><br>
<button type="button" class="btn-left">left</button><button type="button" class="btn-right">Right</button><br>
<button type="button" class="btn-down">Down</button><br>

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You can reuse the statements in your switch as event listeners for the buttons

let inputDirection = {
  x: 0,
  y: 0
}
let lastInputDirection = {
  x: 0,
  y: 0
}



const ArrowUp = () => {
  console.log('hi');
  if (lastInputDirection.y !== 0) return
  inputDirection = {
    x: 0,
    y: -1
  }
};
const ArrowDown = () => {
  if (lastInputDirection.y !== 0) return
  inputDirection = {
    x: 0,
    y: 1
  }
};

const ArrowLeft = () => {
  if (lastInputDirection.x !== 0) return
  inputDirection = {
    x: -1,
    y: 0
  }
};

const ArrowRight = () => {
  if (lastInputDirection.x !== 0) return
  inputDirection = {
    x: 1,
    y: 0
  }
};

let btnUp = document.querySelector(".btn-up")
let btnLeft = document.querySelector(".btn-left")
let btnRight = document.querySelector(".btn-right")
let btnDown = document.querySelector(".btn-down")
btnUp.addEventListener("click",ArrowUp)
btnLeft.addEventListener("click",ArrowLeft)
btnRight.addEventListener("click",ArrowRight)
btnDown.addEventListener("click",ArrowDown)




window.addEventListener('keydown', e => { console.log(e.key)
  switch (e.key) { 
    case 'ArrowUp':
      ArrowUp();
      break
    case 'ArrowDown':
      ArrowDown();
      break
    case 'ArrowLeft':
      ArrowLeft();
      break
    case 'ArrowRight':
      ArrowRight();
      break
  }
})

function getInputDirection() {
  lastInputDirection = inputDirection
  return inputDirection
}
<button type="button" class="btn-up">Up</button><br>
<button type="button" class="btn-left">left</button><button type="button" class="btn-right">Right</button><br>
<button type="button" class="btn-down">Down</button><br>

about 4 years ago · Juan Pablo Isaza Denunciar

0

Set event listeners to your buttons.

function Up() {
  console.log('hi');
  // Do something
}
<button onclick="Up(event)">Up</button>

about 4 years ago · Juan Pablo Isaza Denunciar

0

Everything is working now.

Thanks for the help.

const btnUpOne = document.getElementById('btn-up-one')     
const btnLeftOne = document.getElementById('btn-left-one')     
const btnRightOne = document.getElementById('btn-right-one')     
const btnDownOne = document.getElementById('btn-down-one')

btnUpOne.addEventListener('click', goingUp)    
btnRightOne.addEventListener('click', goingRight)    
btnLeftOne.addEventListener('click', goingLeft)    
btnDownOne.addEventListener('click', goingDown)

//BUTTON MOVEMENTS
//UP
function goingUp(e){
    
  if(lastInputDirection.y !== 0)
  return;

  inputDirection = { x: 0, y: -1 }
  console.log('up');
}

//DOWN
function goingDown(e){

  if(lastInputDirection.y !== 0)
  return;

  inputDirection = { x: 0, y: 1 }
  console.log('down');
  
}

//RIGHT
function goingRight(e){

  if(lastInputDirection.x !== 0)
  return;

  inputDirection = { x: 1, y: 0 }
  console.log('right');   
}

//left
function goingLeft(e){

  if(lastInputDirection.x !== 0)
  return;

  inputDirection = { x: -1, y: 0 }
  console.log('left');
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda