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);
}