HTML and CSS Reference
In-Depth Information
Once this initial phase is complete, the client and the server begin communicating back and forth in real time.
The server sends states, player commands, and server commands. The client sends local player commands to be
echoed to other clients. The following code snippets walk us through an input from the keyboard to the other clients:
Receiving inputs from the keyboard (input.js):
keys: {},
keysToDelete: [],
init: function() {
if (typeof window === 'undefined') {
// Only runs client-side
return;
}
var that = this;
window.addEventListener(
"keydown",
function(e) {
that.keys[e.keyCode] = e.keyCode;
},
false);
window.addEventListener(
'keyup',
function(e) {
// Delay releasing keys to allow the game a chance to read a
// keydown/keyup event, even if the duration is short.
that.keysToDelete.push(e.keyCode);
},
false);
},
getCommands: function(game) {
commands = [];
if ('87' in this.keys) {
commands.push(g.MOVE_UP);
}
if ('68' in this.keys) {
commands.push(g.MOVE_RIGHT);
}
if ('65' in this.keys) {
commands.push(g.MOVE_LEFT);
}
if ('83' in this.keys) {
commands.push(g.MOVE_DOWN);
}
Search WWH ::




Custom Search