Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #37791 > unrolled thread
| Started by | luserdroog <luser.droog@gmail.com> |
|---|---|
| First post | 2019-11-15 10:22 -0800 |
| Last post | 2019-11-16 21:21 +0000 |
| Articles | 6 — 4 participants |
Back to article view | Back to comp.lang.javascript
Game loop losing fast key presses luserdroog <luser.droog@gmail.com> - 2019-11-15 10:22 -0800
Re: Game loop losing fast key presses "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2019-11-15 11:03 -0800
Re: Game loop losing fast key presses luserdroog <luser.droog@gmail.com> - 2019-11-15 21:05 -0800
Re: Game loop losing fast key presses JJ <jj4public@vfemail.net> - 2019-11-16 12:07 +0700
Re: Game loop losing fast key presses luserdroog <luser.droog@gmail.com> - 2019-11-16 12:27 -0800
Re: Game loop losing fast key presses Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-11-16 21:21 +0000
| From | luserdroog <luser.droog@gmail.com> |
|---|---|
| Date | 2019-11-15 10:22 -0800 |
| Subject | Game loop losing fast key presses |
| Message-ID | <d1816a5d-ec3c-4f41-ac3a-c4cd96c8f788@googlegroups.com> |
After spending too much time reading and thinking and not writing code, I decided to make a small game of tetris because I thought I could finish it relatively quickly and add one more thing to my portfolio. But I'm having difficulty with the interaction of the game loop and responding to keyboard input. I have a game loop using window.requestAnimationFrame, which spins a few times until the minimum time slice has elapsed. I also have handlers for keydown and keyup which maintain booleans for the keys of interest. But if I tap a key quickly so the keyup happens while the loop is just spinning, before the next game update, the boolean is false again so the game doesn't react to the key *tap*. The first few pages of google results for "js game loop fast key press lost" don't address this issue at all. Any ideas?
[toc] | [next] | [standalone]
| From | "Michael Haufe (TNO)" <tno@thenewobjective.com> |
|---|---|
| Date | 2019-11-15 11:03 -0800 |
| Message-ID | <32270587-4afa-4cb9-8bfd-b99ba651457c@googlegroups.com> |
| In reply to | #37791 |
On Friday, November 15, 2019 at 12:22:57 PM UTC-6, luserdroog wrote:
> After spending too much time reading and thinking and not writing
> code, I decided to make a small game of tetris because I thought
> I could finish it relatively quickly and add one more thing to my
> portfolio. But I'm having difficulty with the interaction of the
> game loop and responding to keyboard input.
>
> I have a game loop using window.requestAnimationFrame, which spins
> a few times until the minimum time slice has elapsed. I also have
> handlers for keydown and keyup which maintain booleans for the
> keys of interest. But if I tap a key quickly so the keyup happens
> while the loop is just spinning, before the next game update, the
> boolean is false again so the game doesn't react to the key *tap*.
>
> The first few pages of google results for "js game loop fast key press lost"
> don't address this issue at all. Any ideas?
Assuming you have a traditional main loop and pipeline you could have something like the following:
```javascript
...
mainLoop(){
...
clearScreen()
processKeyState()
...
}
...
processKeyState(){
...
if(myKeyManager.upArrow.isPressed()) {
...
// maybe reset state?
}
...
}
```
In other words you aren't listening for a keypress you're checking its state when you're using it.
How you implement the manager and update the state is up to you, but basically document.addEventListener for the key/mouse event and toggle the respective value on your manager.
[toc] | [prev] | [next] | [standalone]
| From | luserdroog <luser.droog@gmail.com> |
|---|---|
| Date | 2019-11-15 21:05 -0800 |
| Message-ID | <4ce4c0e6-c39e-40c2-8564-505424a16023@googlegroups.com> |
| In reply to | #37792 |
On Friday, November 15, 2019 at 1:04:05 PM UTC-6, Michael Haufe (TNO) wrote:
> On Friday, November 15, 2019 at 12:22:57 PM UTC-6, luserdroog wrote:
> > After spending too much time reading and thinking and not writing
> > code, I decided to make a small game of tetris because I thought
> > I could finish it relatively quickly and add one more thing to my
> > portfolio. But I'm having difficulty with the interaction of the
> > game loop and responding to keyboard input.
> >
> > I have a game loop using window.requestAnimationFrame, which spins
> > a few times until the minimum time slice has elapsed. I also have
> > handlers for keydown and keyup which maintain booleans for the
> > keys of interest. But if I tap a key quickly so the keyup happens
> > while the loop is just spinning, before the next game update, the
> > boolean is false again so the game doesn't react to the key *tap*.
> >
> > The first few pages of google results for "js game loop fast key press lost"
> > don't address this issue at all. Any ideas?
>
> Assuming you have a traditional main loop and pipeline you could have something like the following:
>
> ```javascript
> ...
>
> mainLoop(){
> ...
> clearScreen()
> processKeyState()
> ...
> }
> ...
>
> processKeyState(){
> ...
> if(myKeyManager.upArrow.isPressed()) {
> ...
> // maybe reset state?
> }
> ...
> }
> ```
>
> In other words you aren't listening for a keypress you're checking its state when you're using it.
>
> How you implement the manager and update the state is up to you, but basically document.addEventListener for the key/mouse event and toggle the respective value on your manager.
That is essentially what I'm doing. It just feels sluggish because I
can press and release keys with no visible response if the keydown
and keyup events happen too rapidly. But I'm going to leave it until
I have the rest fleshed out. Doubtless there will be many things
that can be improved.
[toc] | [prev] | [next] | [standalone]
| From | JJ <jj4public@vfemail.net> |
|---|---|
| Date | 2019-11-16 12:07 +0700 |
| Message-ID | <g9ru1qe6qi7j.bpid4srhpodm.dlg@40tude.net> |
| In reply to | #37791 |
On Fri, 15 Nov 2019 10:22:49 -0800 (PST), luserdroog wrote: > But if I tap a key quickly so the keyup happens > while the loop is just spinning, before the next game update, the > boolean is false again so the game doesn't react to the key *tap*. Have another boolean variable (e.g. `leftArrowKeyPressed`) to indicate tha a specific key has been pressed. It should be set to `true` by the `keydown` event handler when the event is for the left arrow key. The `keydown` event handler must not modify that variable. The main loop should check that `leftArrowKeyPressed` variable, and after performing task for the left arrow key, acknowledge it by setting it to `false`. No other code should modify that variale unless it's a "game reset" or an "application reset" function.
[toc] | [prev] | [next] | [standalone]
| From | luserdroog <luser.droog@gmail.com> |
|---|---|
| Date | 2019-11-16 12:27 -0800 |
| Message-ID | <8675e9ad-cdfa-4490-9236-c733637de017@googlegroups.com> |
| In reply to | #37797 |
On Friday, November 15, 2019 at 11:07:57 PM UTC-6, JJ wrote: > On Fri, 15 Nov 2019 10:22:49 -0800 (PST), luserdroog wrote: > > > But if I tap a key quickly so the keyup happens > > while the loop is just spinning, before the next game update, the > > boolean is false again so the game doesn't react to the key *tap*. > > Have another boolean variable (e.g. `leftArrowKeyPressed`) to indicate tha a > specific key has been pressed. It should be set to `true` by the `keydown` > event handler when the event is for the left arrow key. The `keydown` event > handler must not modify that variable. > > The main loop should check that `leftArrowKeyPressed` variable, and after > performing task for the left arrow key, acknowledge it by setting it to > `false`. > > No other code should modify that variale unless it's a "game reset" or an > "application reset" function. That's interesting. I think I can make that work. So the fact of the keypress propagates through a few layers until the inner game loop can handle it. If I make it an integer then the keydown can increment it and the game sets it to zero. That way the game can respond to multiple taps when it gets around to it.
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2019-11-16 21:21 +0000 |
| Message-ID | <87pnhrikks.fsf@bsb.me.uk> |
| In reply to | #37800 |
luserdroog <luser.droog@gmail.com> writes: <cut> > That's interesting. I think I can make that work. So the fact of the > keypress propagates through a few layers until the inner game loop can > handle it. > > If I make it an integer then the keydown can increment it and the game > sets it to zero. That way the game can respond to multiple taps when it > gets around to it. Surely you have more than one key that matters? If so, you can't just count the presses because the order will most likely matter. You need a small queue of key actions that gets processed when the app can get round to it. (Jumping in here, so I may have missed the point.) -- Ben.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web