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

120
Vistas
move player with in javascript

I have decided to create a shooter game using HTML, CSS, and Javascript. I created a function that will move the player left to right (horizontally) on the screen. The issue I am encountering is that when I press on the arrow keys nothing happens. I have looked in the internet for advice but nothing helps. The code is at the bottom thank you.

<div class="board">
        <div class="shooter">
            <img src="img/cb.png" alt="" class="shooter-img">
        </div>
    </div>  

const player = document.querySelector('.shooter-img');

function moveRight() {
    let rightPosition = window.getComputedStyle(player).getPropertyValue('right')
    if(player.style.right === "0px"){
return
    }else {
        let position = parseInt(rightPosition)
        position += 4
        player.style.right = '${position}px'
    }
}

function moveLeft() {
    let rightPosition = window.getComputedStyle(player).getPropertyValue('right')
    if(player.style.right ==="360px") {
        return
    }else {
        let position = parseInt(rightPosition)
        position -= 4
        player.style.right = '${position}px'
    }
}

function playerMove(event) {
if(event.key === "ArrowRight") {
    event.preventDefault()
    moveRight()
}else if(event.key === "ArrowLeft") {
    event.preventDefault()
    moveLeft()
}else if(event.key === " ") {
    event.preventDefault()
    fireBullet()
}
}
window.addEventListener('keydown', playerMove);
about 4 years ago · Santiago Trujillo
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