Skip to content

Instantly share code, notes, and snippets.

@iampava
Last active January 24, 2018 08:58
Show Gist options
  • Save iampava/d293a676db4483804a41455d9b34e63a to your computer and use it in GitHub Desktop.
Save iampava/d293a676db4483804a41455d9b34e63a to your computer and use it in GitHub Desktop.
Handling player-move actions by using a queue πŸš€
let actionsQueue = [];
document.addEventListener('keydown', function (e) {
if(37 <= e.keyCode && e.keyCode <= 40) {
//Store only arrow keys
actionsQueue.push(e.keyCode);
}
});
// ...
function paint() {
// ...
let userAction = actionsQueue.shift();
if(userAction !== undefined) {
// If the user hasn't been pressing anything for a while, the queue might be empty
movePlayer(userAction);
}
// ...
}
/** Utility Functions */
// ...
function movePlayer() {
// ...
}
// ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment