I am making a game with easel js and jQuery.
I woud like to handle multiple pressed keys at the same time.
I have this bit of code for one key:
$(document).keydown(function(e){
if (e.keyCode == 37) {
angle=angle+1;
}
if (e.keyCode == 40) {
rad=angle*Math.PI/180;
charachter.x=charachter.x-speed*Math.cos(rad);
charachter.y=charachter.y-speed*Math.sin(rad);
stage.update();
}
if (e.keyCode == 39) {
angle=angle-1;
}
if (e.keyCode == 38) {
rad=angle*Math.PI/180;
charachter.x=charachter.x+speed*Math.cos(rad);
charachter.y=charachter.y+speed*Math.sin(rad);
stage.update();
}
});
But how do I make it so it is rotating and moving foward at the same time?
Both of the events you are looking for will happen inside that listener. If you want to create an action that continues to run after the key has been pressed you need to perform that action inside a loop and activate it with your event listener. so for example ….
then just listen for those keys to go up in a keyup listener and set turn/move to false.