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?
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.