Skip to content

Instantly share code, notes, and snippets.

@thrashr888
Created May 3, 2018 00:49
Show Gist options
  • Save thrashr888/84f5d58a9c3ad2aa0ae0ba80916e914b to your computer and use it in GitHub Desktop.
Save thrashr888/84f5d58a9c3ad2aa0ae0ba80916e914b to your computer and use it in GitHub Desktop.
// respond to key events
Game.Player.prototype.keyUp = function (e) {
delete this._keysPressed[e.key];
};
// respond to key events
Game.Player.prototype.keyDown = function (e) {
this._keysPressed[e.key] = true;
this.dir = {
up: this._keysPressed.w || this._keysPressed.ArrowUp,
left: this._keysPressed.a || this._keysPressed.ArrowLeft,
down: this._keysPressed.s || this._keysPressed.ArrowDown,
right: this._keysPressed.d || this._keysPressed.ArrowRight,
};
if (this.dir.up || this.dir.left || this.dir.down || this.dir.right) {
e.preventDefault();
e.stopPropagation();
}
if (
// refresh page, minimize or print
(e.metaKey === true && (e.key === 'r' || e.key === 'm' || e.key === 'p')) ||
// any browser key commands
(e.altKey === true && e.metaKey === true)
) {
// pass
return;
}
// console.log('Player.keyDown', this.dirCount, e.key, this._keysPressed, e);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment