I'm attempting to make a game with a player character. After making the class with the player's height, width, and spawn location, I decided I wanted to implement movement with the keyboard. I made properties that indicate whether a player is supposed to move in a direction with boolean values but when I want to grab those values with the event listener somehow they become undefined and when I try renderanimationframe of the update method I created, there's an error because the values are undefined. Can anyone explain how these boolean values in my constructor magically became undefined?
const gameWindow = document.querySelector('canvas');
const ctx = gameWindow.getContext('2d');
class Character{
constructor(width,height,x,y,color){
this.height= height;
this.width = width;
this.x = x;
this.y = y;
this.color = color;
this.rightInput = false;
this.leftInput = false;
this.upInput = false;
console.log(this.leftInput,this.upInput,this.rightInput)
}
charMovements(e){
if(e.type=='keydown'){
if(e.key =='d'){
this.rightInput = true;
}
if(e.key == 'a'){
this.leftInput = true;
}
if(e.key =='w'){
this.upInput = true;
}
console.log(this.leftInput,this.upInput,this.rightInput);
}
if(e.type =='keyup'){
if(e.key=='d'){
this.rightInput = false;
}
if(e.key == 'a'){
this.leftInput = false;
}
if(e.key =='w'){
this.upInput = false;
}
}
update(){
// if(this.rightInput == true){
// this.x +=1;
// }
// if(this.leftInput == true){
// this.x-=1;
// }
// if(this.upInput == true){
// this.y += 1;
// }
}
}
let player = new Character(30,30,50,groundLevel,'lightblue');
requestAnimationFrame(player.update);
window.addEventListener('keydown',player.charMovements);
window.addEventListener('keyup',player.charMovements);