HTML and CSS Reference
In-Depth Information
5. We then set the player to be on lane 1 by default:
player.currentLane = 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
} else if (e.keyCode === 39) { // Right
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) {
The following figure shows the key codes of the common arrow keys:
Alternaively, there is reference table on the Internet that allows you to check all the character
codes. The following link contains the character codes:
Search WWH ::

Custom Search