Java Reference
In-Depth Information
that you can use for other games ideas. The last line of code in the
constructor just sets the GameCanvas to be in full-screen mode.
Since your animation runs permanently, it's good practice to put it
in another thread, so we don't block the current thread, which serves
UI application requests. This frees the application to continue processing
events. To do this, we make our GameCanvas Runnable , implement the
game animation loop within the run() method, and use the start()
and stop() methods to start and stop the animation:
public void start()
running = true;
Thread t = new Thread(this);
public void stop()
running = false;
Using t.start() triggers the run() method, which is then responsi-
ble for the game animation. This includes managing background scrolling,
animating the sprite, drawing everything to the screen, and resetting the
background scrolling when needed, so our hero won't fall off the screen:
public void run() {
Graphics graphics = this.getGraphics();
// graphics.setColor(255, 255, 255);
while(running) {
layerY -= 1;
guy.setPosition(getWidth()/2 - guy.getWidth()/2,
getHeight()/2 - guy.getHeight()/2);
try {
} catch(InterruptedException e)
if(layerY == -(background.getHeight() / 2))
layerY = 0;
The source code, JAR and JAD files for the LayerManagerDemo
MIDlet are available from the topic's website. 1 I strongly encourage you
Search WWH ::

Custom Search