style.top и style.left работают только один раз
При первом нажатии на KeyD
или на KeyS
, элемент движется правильно, но при втором элементы перестают двигаться. console.log
пишет, что if-ы используются, а KeyA
и KeyW
вообще не работают. Почему это?
function onMove(event) {
const coordsPlayer = document.getElementById('player');
console.log(coordsPlayer.style.top);
console.log(coordsPlayer.style.left);
if (event.code === "KeyW") {
console.log(coordsPlayer.style.top);
coordsPlayer.style.top -= '25px';
} else if (event.code === 'KeyD') {
console.log(coordsPlayer.style.left);
coordsPlayer.style.left += '25px';
} else if (event.code === 'KeyA') {
console.log(coordsPlayer.style.left);
coordsPlayer.style.left -= '25px';
} else if (event.code === 'KeyS') {
console.log(coordsPlayer.style.top);
coordsPlayer.style.top += '25px';
} else {
console.log('why is it not working?');
}
}
addEventListener('keydown', onMove);