Controls/Input
See also:
System:
Keyboard input
allows for if you are pressing ‘left’ then press ‘right’ at the same time, then let go of ‘right’, ‘left’ will continue – all in the ‘DirectionInput.js’ class.
utilises ‘held directions’ array – e.g.
- when pressing new, valid key, and it isn’t in the array, add to start of array
- when releasing valid key, and its in the array, remove it from the array
We also have a getter so that other items can ask which key is being held right now…
Arrow keys – keep responding while down
Enter key – respond once when pressed and held
We want some buttons to have more of a ‘control ad’ functionality – e.g. press the button, do the thing, then don’t do anything until the button is released, and pressed again.
Arrow Keys – keydown and keyup event listeners
document.addEventListener("keydown", (e) => {
//do the keydown stuff
});
document.addEventListener("keyup", e => {
//do the keyup stuff
});
Enter key
class KeyPressListener {
constructor(keyCode, callback) {
let keySafe = true;
this.keydownFunction = function(event) {
if (event.code === keyCode) {
if(keySafe) {
keySafe = false;
callback();
}
}
};
this.keyupFunction = function(event) {
if (event.code === keyCode) {
keySafe = true;
}
};
}
}
CODE HERE