WAP之家:为您提供最全最新的WAP技术,CP.SP.3G等行业资讯。 WAP之家交流论坛全新开放 点击进入>>
WAP资讯 | 3G动态 | SP动态 | 运营商动态 | 内容商动态 | 制造商动态 | 论坛讨论>> 每次自动访问
WAP技术 | WAP源码 | 手机编程 | 手机源码 | 无线技术 | J2ME技术 | 手机软件 添加到收藏夹
IVR技术 | SP资料 | SMS MMS技术 | 商业方案 | IVR下载 | 书籍教程 | 工具软件 语言:繁體中文

WAP之家技术文章J2ME技术进阶教程使用Game API开发J2ME 2D游戏

使用Game API开发J2ME 2D游戏
作者:mingjava  来源:J2MEDV  发布时间:2005-9-7 12:52:02
便的调用。在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()

上一页  [1] [2] [3]  下一页

[] [返回上一页] [打 印]
文章评论

用户名: 查看更多评论

分 值:100分 85分 70分 55分 40分 25分 10分 0分

内 容:

         (注“”为必填内容。) 验证码: 验证码,看不清楚?请点击刷新验证码