-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunction initializeKeyboardEvents()
More file actions
39 lines (36 loc) · 1.29 KB
/
Copy pathfunction initializeKeyboardEvents()
File metadata and controls
39 lines (36 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// *** No need to edit this page
// Must be called from an Initializationpage to make
// the game responsive to keyboard events
function initializeKeyboardEvents() {
// keyboard press down event
window.onkeydown = function(event) {
var char = getKey(event || window.event);
console.log ("You pressed char="+char);
if (running) {
switch(char) {
case 37 : moveSpriteTo(LEFT_MOVE); event.preventDefault(); event.stopPropagation(); break;
case 38 : moveSpriteTo(UP_MOVE); event.preventDefault(); event.stopPropagation(); break;
case 39 : moveSpriteTo(RIGHT_MOVE); event.preventDefault(); event.stopPropagation(); break;
case 40 : moveSpriteTo(DOWN_MOVE); event.preventDefault(); event.stopPropagation(); break;
}
}
return _isPaused;
};
}
// get character; event.type must be keypress
// this is used for the keyboard control
function getCharKey(event) {
if (event.which === null) {
return event.keyCode; // IE
} else if (event.which!==0 && event.charCode!==0) {
return event.which; // other browsers
} else {
return -1; // special key
}
}
// get character; event.type must be keydown
// this is used for the keyboard control
// and allows for pressing down arrows
function getKey(event) {
return event.keyCode; // IE
}