Java Reference
In-Depth Information
Listing 8-2. Implementing the Game's Control Loop in GameCanvas
public class MyGameCanvas
extends GameCanvas
implements Runnable {
Sprite cat;
public MyGameCanvas () {
super(true);
/* Do other setup here */
}
private void moveCat() {
int keyStates = getKeyStates();
int x, y;
x = cat.getX();
y = cat.getY();
if ((keyStates & LEFT_PRESSED) != 0) {
x-=cat.getWidth()/4;
}
if ((keyStates & RIGHT_PRESSED) != 0) {
x+=cat.getWidth()/4;
}
if ((keyStates & UP_PRESSED) != 0) {
y-=cat.getHeight()/4;
}
if ((keyStates & DOWN_PRESSED) != 0) {
y+=cat.getHeight()/4;
}
cat.setPosition(x,y);
}
private final int DELAYMS = 100;
public void start() {
Thread t = new Thread(this);
t.start();
}
 
Search WWH ::




Custom Search