Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

119
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!