上次我们用Sprite实现了背景,但是这样不便于通过地图数据生成地图。通常,游戏中的地图都被分成NxN的方格,一般仅有几种图案。幸运的是,SUN在MIDP 2.0已经考虑到了游戏开发者需要的这个功能,TiledLayer便是通过贴图实现的层,它的使用同样异常简单!
首先准备好我们需要的小块区域,在坦克大战的地图中,一共有下面5种:
(注意这是放大的jpg格式,你需要处理成40x8的png格式)
分别是冰面,砖块,岩石,河流,树木,具体有什么用玩过的肯定都知道,我就不多说了。
首先读入Image:Image.createImage("/res/img/tile.png");
然后就可以创建TiledLayer了:new TiledLayer(4, 4, image, 8, 8);
前两个参数指定NxN的方格数,后两个参数指定方格大小(也不一定非得是方格),我们用8x8的大小,这样读入的40x8的png图片便被分成了5个小块,编号是1-5。为什么不从0开始编号?因为0是用来表示透明块的。
下面定义一个二维数组,表示地图数据:
int[][] map = new int[][] {
{0, 0, 1, 4},
{1, 1, 2, 4},
{0, 1, 2, 4},
{0, 1, 1, 3},
};
最后用TiledLayer.setCell(col, row, index);就给每个小块贴上对应的图片。如果index为0,则该小块是透明的。
OK,我们把上次的spriteBackground改成layerBackground:
public class TankGameCanvas extends GameCanvas implements Runnable {
...
private TiledLayer layerBackground;
// Constructor and initialization
public TankGameCanvas() {
...
try {
layerBackground = new TiledLayer(22, 22, Image.createImage("/res/img/tile.png"), 8, 8);
int EMP = 0;
int ICE = 1;
int BRK = 2;
int ROC = 3;
int RIV = 4;
int TRE = 5;
// 地图太大了!!!
int[][] map = new int[][] { // 22X22
{EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP},
{EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP},
{ROC, ROC, ROC, ROC, EMP, EMP, EMP, EMP, EMP, EMP, ICE, ICE, ICE, ICE, ICE, ICE, BRK, BRK, BRK, BRK, BRK, BRK},
{ROC, ROC, ROC, ROC, EMP, EMP, EMP, EMP, EMP, EMP, ICE, ICE, ICE, ICE, ICE, ICE, BRK, BRK, BRK, BRK, BRK, BRK},
{BRK, BRK, BRK, BRK, BRK, BRK, BRK, BRK, BRK, BRK, BRK, BRK, ROC, ROC, BRK, BRK, BRK, BRK, ROC, ROC, ROC, ROC},
{BRK, BRK, BRK, BRK, BRK, BRK, BRK, BRK, BRK, BRK, BRK, BRK, ROC, ROC, BRK, BRK, BRK, BRK, ROC, ROC, ROC, ROC},
{ROC, ROC, BRK, BRK, BRK, BRK, EMP, EMP, RIV, RIV, RIV, RIV, RIV, RIV, BRK, BRK, BRK, BRK, EMP, EMP, EMP, EMP},
{ROC, ROC, BRK, BRK, BRK, BRK, EMP, EMP, RIV, RIV, RIV, RIV, RIV, RIV, BRK, BRK, BRK, BRK, EMP, EMP, EMP, EMP},
{ROC, ROC, BRK, BRK, BRK, BRK, EMP, EMP, RIV, RIV, BRK, BRK, BRK, BRK, BRK, BRK, BRK, BRK, BRK, BRK, EMP, EMP},
{ROC, ROC, BRK, BRK, BRK, BRK, EMP, EMP, RIV, RIV, BRK, BRK, BRK, BRK, BRK, BRK, BRK, BRK, BRK, BRK, EMP, EMP},
{ROC, ROC, BRK, BRK, BRK, BRK, EMP, EMP, RIV, RIV, BRK, BRK, BRK, BRK, EMP, EMP, EMP, EMP, BRK, BRK, EMP, EMP},
{ROC, ROC, BRK, BRK, BRK, BRK, EMP, EMP, RIV, RIV, BRK, BRK, BRK, BRK, EMP, EMP, EMP, EMP, BRK, BRK, EMP, EMP},
{EMP, EMP, BRK, BRK, BRK, BRK, EMP, EMP, RIV, RIV, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, ROC, ROC, EMP, EMP},
{EMP, EMP, BRK, BRK, BRK, BRK, EMP, EMP, RIV, RIV, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, ROC, ROC, EMP, EMP},
{ICE, ICE, ICE, ICE, ICE, ICE, BRK, BRK, BRK, BRK, BRK, BRK, ROC, ROC, ROC, ROC, ROC, ROC, ROC, ROC, EMP, EMP},
{ICE, ICE, ICE, ICE, ICE, ICE, BRK, BRK, BRK, BRK, BRK, BRK, ROC, ROC, ROC, ROC, ROC, ROC, ROC, ROC, EMP, EMP},
{BRK, BRK, BRK, BRK, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, BRK, BRK, BRK, BRK, BRK, BRK},
{BRK, BRK, BRK, BRK, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, BRK, BRK, BRK, BRK, BRK, BRK},
{BRK, BRK, BRK, BRK, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, EMP, BRK, BRK, BRK, BRK, EMP, EMP},
{BRK, BRK, BRK, BRK, EMP, EMP, EMP, EMP, EMP, BRK, BRK, BRK, BRK, EMP, EMP, EMP, BRK, BRK, BRK, BRK, EMP, EMP},
{BRK, BRK, BRK, BRK, EMP, EMP, EMP, EMP, EMP, BRK, EMP, EMP, BRK, EMP, EMP, EMP, BRK, BRK, BRK, BRK, EMP, EMP},
{BRK, BRK, BRK, BRK, EMP, EMP, EMP, EMP, EMP, BRK, EMP, EMP, BRK, EMP, EMP, EMP, BRK, BRK, BRK, BRK, EMP, EMP},
};
for(int x=0; x<22; x++) {
for(int y=0; y<22; y++) {
layerBackground.setCell(y, x, map[x][y]);
}
}
}
...
}
...
}
前景也如法炮制,由于手写一个22x22的数组太麻烦,我就没有改了。
编译运行,效果和上次的一样,但是由于有了map[][]数组,地图的生成和存储非常方便,我们能非常容易的判断坦克是否可以移动(条件是对应的map[x][y]<=1,为什么要把冰块放在第一个就是为了判断方便),而且可以很方便地修改地图,比如某块砖被打掉了,便设置map[x][y]=0即可!
(待续... :)
本文地址:http://com.8s8s.com/it/it15733.htm