So what happens is essentially a restriction created by your operating system, but there is a simple approach. I will first explain the restriction, and then the workaround.
If you have been (in the text box), hold down the "j" button, first "j" will appear, and then after a short delay a lot of "j" s will appear: jjjjjjjjjjjjjjjjjjj
This is the problem you are facing. An event fires once, waits a moment, and then fires many more times.
The solution, however, is simple. Instead of your wheel moving when events were triggered, constantly update it and separately monitor which keys are up or down.
The key handler will look something like this:
function KeyHandler() { this.left = false; this.right= false; ... function onKeyDown(e) { if (e.keyCode == 37) { this.left = true; } ... } function onKeyUp(e) { if (e.keyCode == 37) { this.left = false; } ... } }
(you attach the key handler to the body or any other element that you want)
Your wheel will have an update function that would look like ...
wheel.update = function() {
and then the game will have something like this ...
wheel = new Wheel; setInterval(function() { wheel.update(); },100);
Thus, your wheel will always be updated depending on the current state of the keys, and you will not have to rely on the limitations of the events that fire. :)
zconnelly13
source share