便的调用。在Sprite中还提供了碰撞检测的函数,你可以选择像素级别的检测或者矩形边框界别的检测。通常后者比较简单,但是粗糙一些。前者精确但是速度慢。
下面通过一个简单的游戏例子来介绍如何使用这些类来开发J2ME 2D游戏,您可以参考SUN的文章Creating 2D Actions Games with the game API。
在这个程序中主要有两个对象,一个是坦克一个是背景,我们分别采用Sprite和TiledLayer类来构建。通常我们在Sprite派生出来的类中定义好动作,这样我们在GameCanvas里面可以很容易接受用户的输入事件然后处理了。
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
public class MicroTankSprite extends Sprite
{
private int mDirection;
private int mKX, mKY;
private int mLastDelta;
private boolean mLastWasTurn;
private static final int[] kTransformLookup = { Sprite.TRANS_NONE,
Sprite.TRANS_NONE, Sprite.TRANS_NONE, Sprite.TRANS_MIRROR_ROT90,
Sprite.TRANS_ROT90, Sprite.TRANS_ROT90, Sprite.TRANS_ROT90,
Sprite.TRANS_MIRROR_ROT180, Sprite.TRANS_ROT180,
Sprite.TRANS_ROT180, Sprite.TRANS_ROT180,
Sprite.TRANS_MIRROR_ROT270, Sprite.TRANS_ROT270,
Sprite.TRANS_ROT270, Sprite.TRANS_ROT270, Sprite.TRANS_MIRROR };
private static final int[] kFrameLookup = { 0, 1, 2, 1, 0, 1, 2, 1, 0, 1,
2, 1, 0, 1, 2, 1 };
private static final int[] kCosLookup = { 0, 383, 707, 924, 1000, 924, 707,
383, 0, -383, -707, -924, -1000, -924, -707, -383 };
private static final int[] kSinLookup = { 1000, 924, 707, 383, 0, -383,
-707, -924, -1000, -924, -707, -383, 0, 383, 707, 924 };
public MicroTankSprite(Image image, int frameWidth, int frameHeight)
{
super(image, frameWidth, frameHeight);
defineReferencePixel(frameWidth / 2, frameHeight / 2);
mDirection = 0;
}
public void turn(int delta)
{
mDirection += delta;
if (mDirection < 0)
mDirection += 16;
if (mDirection > 15)
mDirection %= 16;
setFrame(kFrameLookup[mDirection]);
setTransform(kTransformLookup[mDirection]);
mLastDelta = delta;
mLastWasTurn = true;
}
public void forward(int delta)
{
fineMove(kCosLookup[mDirection] * delta, -kSinLookup[mDirection]
* delta);
mLastDelta = delta;
mLastWasTurn = false;
}
public void undo()
{
if (mLastWasTurn)
turn(-mLastDelta);
else
forward(-mLastDelta);
}
private void fineMove(int kx, int ky)
{
// First initialize mKX and mKY if they're
// not close enough to the actual x and y.
int x = getX();
int y = getY();
int errorX = Math.abs(mKX - x * 1000);
int errorY = Math.abs(mKY - y * 1000);
if (errorX > 1000 || errorY > 1000)
{
mKX = x * 1000;
mKY = y * 1000;
}
// Now add the deltas.
mKX += kx;
mKY += ky;
// Set the actual position.
setPosition(mKX / 1000, mKY / 1000);
}
}
在GameCanvas中我们的程序流程如下
public void run()
{
Graphics g = getGraphics();
int timeStep = 80;
while (mTrucking)
{
long start = System.currentTimeMillis();
tick();
input();
render(g);
long end = System.currentTimeMillis();
int duration = (int) (end - start);
if (duration < timeStep)
{
try
{
Thread.sleep(timeStep - duration);
} catch (InterruptedException ie)
{
stop();
}
}
}
}
基本的思路就是接受用户事件,重新绘制屏幕。
import java.io.IOException;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
public class MicroTankCanvas extends GameCanvas implements Runnable
{
private volatile boolean mTrucking;
private MicroTankSprite mTank;
private TiledLayer mBoard;
private LayerManager mLayerManager;
public MicroTankCanvas() throws IOException
{
super(true);
mTank = createTank();
mTank.setPosition(0, 24);
mBoard = createBoard();
mLayerManager = new LayerManager();
mLayerManager.append(mTank);
mLayerManager.append(mBoard);
}
private MicroTankSprite createTank() throws IOException
{
Image image = Image.createImage("/tank.png");
return new MicroTankSprite(image, 32, 32);
}
private TiledLayer createBoard() throws IOException
{
Image image = Image.createImage("/board.png");
TiledLayer tiledLayer = new TiledLayer(10, 10, image, 16, 16);
int[] map = { 1, 1, 1, 1, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 7, 1, 0, 0, 0, 0, 0, 1,
1, 1, 1, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 11, 0, 0, 0,
0, 0, 0, 0, 7, 6, 0, 0, 0, 0, 0, 0, 0, 7, 6, 0, 0, 0 };
for (int i = 0; i < map.length; i++)
{
int column = i % 10;
int row = (i - column) / 10;
tiledLayer.setCell(column, row, map[i]);
}
return tiledLayer;
}
public void start()
{
mTrucking = true;
Thread t = new Thread(this);
t.start();
}
public void run()
{
Graphics g = getGraphics();
int timeStep = 80;
while (mTrucking)
{
long start = System.currentTimeMillis();
tick();
input();
render(g);
long end = System.currentTimeMillis();
int duration = (int) (end - start);
if (duration < timeStep)
{
try
{
Thread.sleep(timeStep - duration);
} catch (InterruptedException ie)
{
stop();
}
}
}
}
private void tick()
{
if (mTank.collidesWith(mBoard, true))
mTank.undo();
}
private void input()