I don’t know how to make the circle move.
window.addEventListener(‘keypress,’,typeKey);
function typeKey(e){
if(e.key==='w'||e.key==='a'||e.key==='d'||e.key==='d'){
//I don't know how to make the circle move.
}
I don’t know how to make the circle move.
window.addEventListener(‘keypress,’,typeKey);
function typeKey(e){
if(e.key==='w'||e.key==='a'||e.key==='d'||e.key==='d'){
//I don't know how to make the circle move.
}
may be the comma after keypress → ‘keypress,’ ?!
The tip was helpful but I can’t get the circle to move every time I press a key.
could you please share the full code
Hi @Joaquin_Gomez, and welcome to our community!
So for this code, inside the event handler you need to do four separate tests, one for each key that you are interested in being pressed. In each case, you want to alter the value of x
or y
so that the circle is drawn higher or lower on each key press, effectively moving it up/down/left/right.
a version using a switch statement might look like this:
window.addEventListener('keypress', (e) => {
switch(e.key) {
case 'a':
x -= 2;
break;
case 'd':
x += 2;
break;
case 'w':
y -= 2;
break;
case 's':
y += 2;
break;
}
drawCircle(x, y, size);
});
Thank you very much for the clarification