0

If you didn't understand the question, try the following: go to the input field in the comments and then hold a key on your keyboard. You will see that it will do something like the following: "x - (small stutter) - xxxxxxxxxxxxxxxxxxxxxxx...". I'm pretty sure that small stutter is because it's going from "single character" mode to "hold down the character" mode, but is there a way to bypass this small stutter? I'm creating a 2d car, and the stutter causes the car to move a bit, stop, and then continue driving. Here is the part of my code the stutter is affecting:

document.body.addEventListener("keydown", function () {
    drive = true;
    if (drive) {
        carLeft += 50;
        speed += 5;
        car.style.left = carLeft + "px";
    }
    if (carLeft >= 1900) {
        alert("You left the playing area!");
        break;
    }
});

I'm sorry if this is a poor question/poorly formatted question, I don't have a lot of time to ask this. Thanks in advance!

pierreh
  • 159
  • 8
  • Cannot reproduce. – Wais Kamal Oct 16 '20 at 18:16
  • Yes you can maybe this will help https://jeremyckahn.github.io/keydrown/ – Alpha Wolf Gamer Oct 16 '20 at 18:19
  • I don't have a lot of time to answer this, so https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/repeat – Heretic Monkey Oct 16 '20 at 18:21
  • This has to do with your keyboard's autorepeat delay and autorepeat rate. This has nothing to do with javascript and thus can't be mitigated in the language. – Aplet123 Oct 16 '20 at 18:21
  • 1
    Does this answer your question? [JavaScript move delay and multiple keystrokes](https://stackoverflow.com/questions/11197220/javascript-move-delay-and-multiple-keystrokes) – David Oct 16 '20 at 18:21
  • drive = true; if (drive) that is ambiguous if() – Stefan Avramovic Oct 16 '20 at 18:34
  • You need to build a tick function instead of relying on the keyboard repeat rate to control the flow of your game. If someone has a slower or faster repeat rate, it would affect the rate of acceleration in the car with in the code sample you provided. – Our_Benefactors Oct 16 '20 at 20:14

1 Answers1

2

kd.P.down(function() {
  document.body.innerHTML += 'p is holded down<br>'
});

kd.P.up(function() {
  document.body.innerHTML = ''
});

// This update loop is the heartbeat of Keydrown
kd.run(function() {
  kd.tick();
});
<script src="https://jeremyckahn.github.io/keydrown/dist/keydrown.min.js"></script>

This will do the trick. Run the snippet click on it so you are focusing of the snippet and hold down the letter p the shutter you want to bypass is gone!!

For more info go to this link

https://jeremyckahn.github.io/keydrown/

Alpha Wolf Gamer
  • 320
  • 2
  • 12