使用Game API开发J2ME 2D游戏[4]

[入库:2006年2月23日] [更新:2007年3月24日]

本文简介:

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

本文关键:使用Game API开发J2ME 2D游戏
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top