Java Reference
In-Depth Information
public void run() {
Graphics g = getGraphics();
while(true) {
updateGameState(); // move computer controlled characters
moveCat();
// poll for events, move main character
doPaint();
// paint screen
try {
Thread.sleep(DELAYMS);
}
catch(InterruptedException ex) {}
}
}
}
In this more-than-pseudocode, less-than-a-game, the polling occurs in the moveCat
method I showed you in the previous section. Once the thread is started, the thread
updates the game's characters, polls the keys, and repaints the screen every DELAYMS (100
milliseconds). The updateGameState and doPaint methods aren't defined yet, of course—
more on them as we continue to explore the API.
Tying Your GameCanvas to Your MIDlet
As you remember from the discussion of the Canvas class in Chapter 5, it and its sub-
classes implement Displayable , meaning you can set the GameCanvas subclass to be the
active Displayable using the Display 's setCurrent method, as shown in Listing 8-3.
Listing 8-3. Setting the GameCanvas to Be the Active Displayable
public class GameCanvasSampleMIDlet extends MIDlet {
private MyGameCanvas canvas;
public GameCanvasSampleMIDlet () {
}
private void initialize() {
canvas = new MyGameCanvas();
getDisplay().setCurrent(canvas);
canvas.start();
}
 
Search WWH ::




Custom Search