How do I do smooth movement in JS Graphics on CodeHS. Everything I see when I try to Google it is for when you’re doing JS with HTML.
Here’s my current code
let centerX = getWidth()/2;
let centerY = getHeight()/2;
let sqr;
function start(){
createSquare();
keyDownMethod(move);
}
function createSquare(){
sqr = new Rectangle(50,50);
sqr.setPosition(centerX-50/2,centerY-50/2)
add(sqr);
}
function move(e){
if(e.keyCode == Keyboard.letter('W') || e.keyCode == Keyboard.UP){
sqr.move(0,-5);
} else if(e.keyCode == Keyboard.letter('S') || e.keyCode == Keyboard.DOWN){
sqr.move(0,5);
} else if(e.keyCode == Keyboard.letter('A') || e.keyCode == Keyboard.LEFT){
sqr.move(-5,0);
} else if(e.keyCode == Keyboard.letter('D') || e.keyCode == Keyboard.RIGHT){
sqr.move(5,0);
}
}
At first I tried putting a move 1 in a for loop but it still basically just snaps to a position. I also tried doing that but with setTimeout but instead of waiting for the amount of milliseconds I put each time it loops it just waits and then does the loop the set amount of times even though the set time out is inside of the loop.