Hi,
I rewrote an example from the good book "Killer Game Programming in Java" and now have a question about design.
The main setup is as follows:
There is JPanel implementing the interface runnable. The method run looks like this:
1 2 3 4 5
| while (running) { gameUpdate(); gameRender(); paintScreen(); |
Then follow some rather sophistiacted techniques for sleeping and skipping frames
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| if (sleepTime > 0) { try { Thread.sleep(sleepTime / 1000000L); } catch (InterruptedException ex) { } overSleepTime = (System.nanoTime() - afterTime) - sleepTime; } else { excess -= sleepTime; overSleepTime = 0L;
if (++noDelays >= NO_DELAYS_PER_YIELD) { Thread.yield(); noDelays = 0; } }
beforeTime = System.nanoTime(); |
1 2 3 4 5 6 7
| int skips = 0; while ((excess > period) && (skips < MAX_FRAME_SKIPS)) { excess -= period; gameUpdate(); skips++; } framesSkipped += skips; |
I added a KeyListener to the JPanel and in the gameUpdate() method I added
1 2
| if (keyPressed) { worm.moveLeft() |
It works, but somehow some keyEvents are not processed. When I press 'down' the worm moves down, but every third time or so it doesn't move.
How can I solve this? What is the principle design pattern?
Should I add some message coming from the worm saying moving was performed successfully?
I'll provide more code if necessary.
Thx for any help.