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

Keyboard handler for moving in four directions

$
0
0

I'm having trouble deciding if I should leave the default: break; in the switch statement or not. It does kinda show intent, i.e. that most keys are not handled, but it's also effectively useless code. I feel like it's clear enough anyway, but maybe being explicit is better?

function handleKeyDownEvent(event) {    switch (event.keyCode) {        case 87: // w            player.dy = -50            break        case 65: // a            player.dx = -50            break        case 83: // s            player.dy = 50            break        case 68: // d            player.dx = 50            break        default:            break    }}

Maps seem to be the preferred way of doing this, instead of a switch statement. How does the movement map work with the corresponding handleKeyUpEvent which works the same way, except that it zeroes the dx or dy values?

Should I keep the default: break; or not?


Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles





Latest Images