0

slight disclaimer, i'm relatively new to JavaScript and i'm not the best, although I guess that begs the question how do you define "best". Anyway I digress. My question is when I am moving an object around the canvas why is it slow (stuttery) and only one key press can be registered at one time. Is there a way to make it more smooth and so that you can register multiple keys at a time?

// player movement

window.onkeydown = function playermovement () {
var keypress = event.which || event.keyCode; 

//up
if (keypress == 87 && player.y >0) {
player.y = player.y -20
}


//down
if (keypress == 83 && player.y < 400) {
player.y = player.y + 20
}

if (keypress == 38) {
player2.y = player2.y -20
}

if (keypress == 40) {
player2.y = player2.y +20
}

}

Thank you

James

James Taylor
  • 29
  • 1
  • 4
  • 2
    For the multiple keys part: http://stackoverflow.com/questions/5203407/javascript-multiple-keys-pressed-at-once – XCS Nov 12 '16 at 18:30
  • And for the smoother movement you would need some sort of animation loop, or interpolation to move the object only a few pixels each frame while the key is being pressed down. – XCS Nov 12 '16 at 18:31

0 Answers0