Something seems off with my code. I have an image displayed on a canvas, but when I press the specified key, nothing happens. Can you help me figure out where I went wrong?
let img = document.getElementById("ship");
let player = {
x: 375,
y: 550,
w: 50,
h: 50,
};
requestAnimationFrame(draw);
function draw() {
//Logic
ctx.drawImage(img, player.x, player.y, player.w, player.h);
//Draw
ctx.clearRect(0, 0, cnv.width, cnv.height);
requestAnimationFrame(draw);
}
document.addEventListener("keydown", move);
function move(event) {
console.log(event.code);
if (event.code === 39) {
player.x = player.x + 50;
} else if (event.code === 37) {
player.x = player.x - 50;
} else if (event.code === 40) {
player.x = player.y - 50;
} else if (event.code === 38) {
player.x = player.y + 50;
}
}