Student post

Weekly Article 2

Fixing player movement

Apr 17, 2026 Togore #game-development#movement
Back to /students/togore-dreemurr/

Originally, diagonal movement to the left moved faster than to the right. I was overcomplicating the code, which unboudtedly caused the problem.

Oringinal Code:

function moveX(spd) {
  player.style.left = parseInt(player.style.left) + spd + "px";
}
function moveY(spd) {
  player.style.top = parseInt(player.style.top) + spd + "px";
}

I created a new function for diagonal movement and entirely rewrote the movement code into:

function moveDiagonal(spd, xDir, yDir) {
  let diagonalSpeed = spd / Math.sqrt(2);
  playerX += diagonalSpeed * xDir;
  playerY += diagonalSpeed * yDir;
  player.style.left = playerX + "px";
  player.style.top = playerY + "px";
}

Calculating the player’s position in small chunks corrected the error.