上一篇中提到了GameCanvas 对象了,现在我们就来创建一个基于它的扩展类,起个名字叫TankGameKanvas,看代码前先介绍几个对象,
第一:Sprite对象,这个对象主要用来表现我们操纵的对象,比如本例中的坦克,Sprite有很多的方法和属性,我们这里用到的只有简单的几个
更详细的可以参考sun的midp2.0文档。
Sprite(Image image, int frameWidth, int frameHeight)
Creates a new animated Sprite using frames contained in the provided Image.
使用给定的图像的不同的帧来创建一个活动的家伙。
setFrame(int sequenceIndex)
Selects the current frame in the frame sequence.
我们用的图片(放大后)如下
(64pxX16px) 这样我们的Sprite对象就象一个白纸上挖了个16x16的孔,通过移动到原图的不同位置来显示坦克的
不同方向。关于原图文件有比较严格的要求,需要时png图像格式的,其他的都不可以。
现在看看代码如何实现吧。
//TankGameKanvas.java
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
public class TankGameCanvas extends GameCanvas implements Runnable
{
// 控制方向:
private static int INDEX_OF_UP = 0;
private static int INDEX_OF_DOWN = 1;
private static int INDEX_OF_LEFT = 3;
private static int INDEX_OF_RIGHT = 2;
![](/Images/OutliningIndicators/InBlock.gif)
private boolean isPlay;
private long delay;
private int currentX, currentY;
private int width;
private int height;
![](/Images/OutliningIndicators/InBlock.gif)
private Sprite spriteTank;
//构造函数
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public TankGameCanvas()
{
super(true);
width = getWidth(); //获取屏幕宽度
height = getHeight(); //获取屏幕高度
currentX = width / 2; //当前X坐标 居中
currentY = height / 2; //当前Y坐标 居中
delay = 20;
![](/Images/OutliningIndicators/InBlock.gif)
//看看我们的Sprite吧
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
Image image = Image.createImage("/res/img/player1.png"); // 注意路径,和文件格式
spriteTank = new Sprite(image, 16, 16); // 大小是16x16
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
catch(Exception e)
{ e.printStackTrace(); }
}
//当开始的时候显示参数为真,并且再次启动线程,--这样就循环起来了。
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void start()
{
isPlay = true;
new Thread(this).start();
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void stop()
{ isPlay = false; }
![](/Images/OutliningIndicators/InBlock.gif)
// -重要的是这里设置了线程,设置了启动间隔时间20毫秒
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void run()
{
Graphics g = getGraphics();
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
while (isPlay)
{
input();
drawScreen(g);
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
Thread.sleep(delay);
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
catch (InterruptedException ie)
{}
}
}
![](/Images/OutliningIndicators/InBlock.gif)
// 对用户输入的捕获和处理
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
private void input()
{
int keyStates = getKeyStates();
// Left
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if ((keyStates & LEFT_PRESSED) != 0)
{
currentX = Math.max(0, currentX - 1);
spriteTank.setFrame(INDEX_OF_LEFT);
}
// Right
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if ((keyStates & RIGHT_PRESSED) !=0 )
{
if ( currentX + 5 < width)
currentX = Math.min(width, currentX + 1);
spriteTank.setFrame(INDEX_OF_RIGHT);
}
// Up
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if ((keyStates & UP_PRESSED) != 0)
{
currentY = Math.max(0, currentY - 1);
spriteTank.setFrame(INDEX_OF_UP);
}
// Down
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if ((keyStates & DOWN_PRESSED) !=0)
{
if ( currentY + 10 < height)
currentY = Math.min(height, currentY + 1);
spriteTank.setFrame(INDEX_OF_DOWN);
}
}
// 显示
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
private void drawScreen(Graphics g)
{
g.setColor(0); // black
g.fillRect(0, 0, getWidth(), getHeight());
![](/Images/OutliningIndicators/InBlock.gif)
spriteTank.setPosition(currentX, currentY);
spriteTank.paint(g);
flushGraphics();
}
}
编译一下,你的小坦克应该可以来回乱跑了。;)