HTML and CSS Reference
In-Depth Information
5. We then set the player to be on lane 1 by default:
player.currentLane = 1;
player.changeLane(1);
6. Next, we get the keyboard input in the game scene. Put the following code inside
the setup method in the game-scene.js ile:
window.onkeyup = function(e) {
if (e.keyCode === 37) { // Left
game.player.moveToLeftLane();
} else if (e.keyCode === 39) { // Right
game.player.moveToRightLane();
}
return false;
};
Objective complete - mini debriefing
We have added keyboard inputs to control the player.
Each key on the keyboard is assigned one ID. In JavaScript, the keyboard event handler allows
us to access the unique ID for each key. This way, we know what key the player has pressed.
In our game, we need the game to listen for the left and right keys. To know which key to
press, we can print the value of any pressed keys and observe the key code in the console:
window.onkeyup = function(e) {
console.log(e.keyCode);
};
The following figure shows the key codes of the common arrow keys:
38
37
39
40
Alternaively, there is reference table on the Internet that allows you to check all the character
codes. The following link contains the character codes:
http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-
codes
 
Search WWH ::




Custom Search