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);
t.start();
}
{
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;
background.setPosition(0,layerY);
guy.setPosition(getWidth()/2 - guy.getWidth()/2,
getHeight()/2 - guy.getHeight()/2);
manager.paint(graphics,0,0);
flushGraphics();
guy.nextFrame();
try
{
Thread.sleep(20);
}
catch(InterruptedException e)
{
break;
}
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
1
developer.symbian.com/javameonsymbianos