aspose file tools*
The moose likes Game Development and the fly likes KeyListener press/release problem. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Game Development
Bookmark "KeyListener press/release problem." Watch "KeyListener press/release problem." New topic
Author

KeyListener press/release problem.

Olivier Legat
Ranch Hand

Joined: Nov 17, 2007
Posts: 176

Hello folks,

Me and my parnter are making a Tetris game for a project and have encountered a problem concerning the KeyListener class and we're aren't quite sure how to get around it. I won't post ALL the code but here's the essential:



The problem we a have is for the "Speed up" control. We want the currently controlled Tetrimino (a "building-block" so to speak) to speed up immediately when the key is pressed, as well as to slow down as soon as the user releases of the key (so is the purpose of the interrupt() in the speed control functions of Gravity).

When I press down on the Down key (default controls) the tetrimino speeds up immediately but after a while stops moving completely. It only starts to move after if I release the Down key or do something else with it (such as rotate or move it). I'd assume that this is because after holding the key for a while the OS "spams" that key so speak (in other words, it repetitively invokes a "pressed" command for that key), which would evidently cause the Gravity thread to be interrupted non-stop, thus not allowing it to let a piece fall.

I can fix this problem by remove the "interrupt()" calls but this causes the speed up command to be delayed, which is even more annoying. Any ideas on how to go about this problem?

Thank-you.


Olly
Paul Beckett
Ranch Hand

Joined: Jun 14, 2008
Posts: 96
The first thing I'd probably do is change it so you do as little as possible on the keyPressed event. So you hold an object that stores the state of the keys (actions) and then in your main game loop, you check the state of those actions and handle them.




What I've described above will have the problem that speedUp() will be called once for each frame when you have the "speedUp" key pressed - probably not desirable. One way to get around this is to only check the first key press and then reset back to false again.

Also I've taken these ideas from Killer Game Programming (sample chapters available from the website) and Developing Games in Java. I'd recommend reading the early chapters of both books to help get you started.

Olivier Legat
Ranch Hand

Joined: Nov 17, 2007
Posts: 176

Thank you Paul Beckett, your idea of using a private boolean solved the problem . However I don't understand why you are putting e.consume(). Here it says:
Java API wrote:Consumes this event so that it will not be processed in the default manner by the source which originated it.

But I don't quite understand what that means. Could someone explain this for me please.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: KeyListener press/release problem.