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:
while (running) {
gameUpdate();
gameRender(); // render the game to a buffer
paintScreen(); // draw the buffer on-screen
Then follow some rather sophistiacted techniques for sleeping and skipping frames
if (sleepTime > 0) { // some time left in this cycle
try {
// sleep a bit,
Thread.sleep(sleepTime / 1000000L); // nano -> ms
} catch (InterruptedException ex) {
}
overSleepTime = (System.nanoTime() - afterTime) - sleepTime;
} else { // sleepTime <= 0; the frame took longer than the period
excess -= sleepTime; // store excess time value
overSleepTime = 0L;
// count the frames with no delays and yield
if (++noDelays >= NO_DELAYS_PER_YIELD) {
Thread.yield(); // give another thread a chance to run
noDelays = 0;
}
}
beforeTime = System.nanoTime();
int skips = 0;
while ((excess > period) && (skips < MAX_FRAME_SKIPS)) {
excess -= period;
gameUpdate(); // update state but don't render
skips++;
}
framesSkipped += skips;
I added a KeyListener to the JPanel and in the gameUpdate() method I added
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.