I'm trying to make a video game with Javascript, HTML and css, a platformer. But I encountered a difficulty with the eternal jump on 'w', I have a "gravity that get me down if I don't do anything. So I thought it was possible to get:
function up() {
document.addEventListener("keydown", function (event) {
if (event.key === "w" || event.key === "ArrowUp") {
upKey = true;
but it must work only for x seconds so it's a limited jump, any ideas are welcomed. I tried with principe:
function execOnce(fn, context) {
var result;
return function () {
if (fn) {
result = fn.apply(context || this, arguments);
fn = null;
}
return result;
};
}
function sayHello() {
console.log("hello world");
}
var helloOnce = execOnce(sayHello);
helloOnce(); // hello world
helloOnce(); // nothing but emptiness
helloOnce();
helloOnce();
But my variables are true and not a number so I don't know how to do. Thank you (yes I'm new to JS)