Keydown bouncing ball

window.addEventListener('keydown', (e) => {

switch(e.key) {
case ‘a’:
this.x -= this.velX;
break;
case ‘d’:
this.x += this.velX;
break;
case ‘w’:
this.y -= this.velY;
break;
case ‘s’:
this.y += this.velY;
break;
}
})

why is this code block placed inside the constructor code block

Hi @samuelharold327

It’s inside the EvilCircle constructor for the key bindings to be automatically created when the evil circle gets created. You couldn’t use it outside the EvilCircle class either, because we need the this keyword to refer to the evil circle. Otherwise, key presses wouldn’t move the evil circle.

Cheers,
Michael

2 Likes

helpfully. thank you.

1 Like