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

121
Visualizações
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 à 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