Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #37796

Re: Game loop losing fast key presses

Newsgroups comp.lang.javascript
Date 2019-11-15 21:05 -0800
References <d1816a5d-ec3c-4f41-ac3a-c4cd96c8f788@googlegroups.com> <32270587-4afa-4cb9-8bfd-b99ba651457c@googlegroups.com>
Message-ID <4ce4c0e6-c39e-40c2-8564-505424a16023@googlegroups.com> (permalink)
Subject Re: Game loop losing fast key presses
From luserdroog <luser.droog@gmail.com>

Show all headers | View raw


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.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web