javascript wasd code example
Example: wasd controls javascript
function MyKeyUpHandler (MyEvent) {
if (MyEvent.keyCode == 65 || MyEvent.keyCode == 68) {hero.velocity_x= 0};
if (MyEvent.keyCode == 87 || MyEvent.keyCode == 83) {hero.velocity_y= 0};
}
function MyKeyDownHandler (MyEvent) {
if (MyEvent.keyCode == 65) {hero.velocity_x= -Quickness};
if (MyEvent.keyCode == 87) {hero.velocity_y= -Quickness};
if (MyEvent.keyCode == 68) {hero.velocity_x= Quickness};
if (MyEvent.keyCode == 83) {hero.velocity_y= Quickness};
MyEvent.preventDefault();
}
function MyTouchHandler (MyEvent) {
var rect = myCanvas.getBoundingClientRect();
hero.velocity_y= 0;
hero.velocity_x= 0;
for (var i=0; i < MyEvent.touches.length; i++) {
var x = MyEvent.touches[i].clientX - rect.left;
var y = MyEvent.touches[i].clientY - rect.top;
if (x > myCanvas.width * 0.66) hero.velocity_x= hero.velocity_x + Quickness;
if (x < myCanvas.width * 0.33) hero.velocity_x= hero.velocity_x - Quickness;
if (y > myCanvas.height * 0.66) hero.velocity_y= hero.velocity_y + Quickness;
if (y < myCanvas.height * 0.33) hero.velocity_y= hero.velocity_y - Quickness;
}
MyEvent.preventDefault();
}