Quantcast
Channel: Keyboard handler to move a shape in response to arrow keys - Code Review Stack Exchange
Viewing all articles
Browse latest Browse all 3

Answer by jfriend00 for Keyboard handler to move a shape in response to arrow keys

$
0
0

If you move shipLeft and shipTop into an object called shipPositions rather than just top level variables, you could also use a map-driven approach like this:

function anim(e) {    var key = e.KeyCode || e, val, info;    var keyMap = {"37": {direction: "left", ship: "shipLeft", delta: -11},"39": {direction: "left", ship: "shipLeft", delta: 11},"40": {direction: "top", ship: "shipTop", delta: 11},"38": {direction: "top", ship: "shipTop", delta: -11},    };    info = keyMap[key];    if (!info) {        return;    }    shipPositions[info.ship] += info.delta;    val = shipPositions[info.ship];    ship.style[info.direction] = val +'px';    changeColor();    return val;}

Viewing all articles
Browse latest Browse all 3

Trending Articles



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