Capturing User Interaction

The Brain Eaters game supports keyboard, mouse, touch, and stylus interaction. If you are using the keyboard then you can move your character by using the arrow keys.

The following code is used to capture keyboard interaction:

document.addEventListener("keydown", this.movePlayerKeyboard.bind(this));

When you press a key down, the movePlayerKeyboard() method is called. This method detects the arrow key that was pressed and changes the direction of the player:

movePlayerKeyboard: function (e) {    switch (e.keyCode) {        case WinJS.Utilities.Key.upArrow:            this._player.direction = Unleashed.Direction.up;            break;        case WinJS.Utilities.Key.downArrow: ...

Get Windows® 8.1 Apps with HTML5 and JavaScript Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.