Last active
January 24, 2018 08:58
-
-
Save iampava/d293a676db4483804a41455d9b34e63a to your computer and use it in GitHub Desktop.
Handling player-move actions by using a queue π
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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