Quantcast
Channel: Keyboard handler for moving in four directions - Code Review Stack Exchange
Viewing all articles
Browse latest Browse all 3

Answer by 200_success for Keyboard handler for moving in four directions

$
0
0

In this case, I think that default: would be noise. It's obvious that this code makes no attempt to handle every possible keycode.

I suggest using a variant of @SirPython's suggestion to use a lookup table. It can be written in a way that reminds you of the keyboard layout.

var MOVEMENT = {    /* W */ 87: { dy: -50 },  /* A */ 65: { dx: -50 },              /* D */ 68: { dx: +50 },                    /* S */ 83: { dy: +50 },};function handleKeyDownEvent(event) {    var movement;    if (movement = MOVEMENT[event.keyCode]) {        player.dx += (movement.dx || 0);        player.dy += (movement.dy || 0);    }}

Viewing all articles
Browse latest Browse all 3

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>