通常卷轴设计类的游戏都会有好几个关口,或者叫做等级。不同的关口的背景地图是不一样的。这里简单起见,设立两个关口。
这时遇到的一个问题是,不同的关口对应不同的背景地图,而将这些设定都放在GameScreen类里就不怎么合适了。为了便于管理和以后的扩展,新建一个GameMap类来管理背景地图。
public GameMap(int screenHeight) throws Exception {
this.screenHeight = screenHeight;
setMap(2); // default to set to terrain 1
}
// Set Appropriate Terrain and Map
public void setMap(int level) throws Exception {
Image tileImages = null;
switch (level) {
case 1:
tileImages = Image.createImage("/terrain1.png");
currentMap = map1;
groundColor = 0xF8DDBE;
break;
case 2:
tileImages = Image.createImage("/terrain2.png");
currentMap = map2;
groundColor = 0xDECE6B;
break;
default:
tileImages = Image.createImage("/terrain1.png");
currentMap = map1;
groundColor = 0xF8DDBE;
break;
}
terrain = new TiledLayer(TILE_NUM_COL, TILE_NUM_ROW, tileImages,
TILE_WIDTH, TILE_HEIGHT);
// Map Terrain Map with actual graphic from terrain.png
for (int row = 0; row < TILE_NUM_ROW; row++) {
for (int col = 0; col < TILE_NUM_COL; col++) {
terrain.setCell(col, row, currentMap[row][col]);
}
}
terrainScroll = 1 - (terrain.getCellHeight() * terrain.getRows())
+ screenHeight;
terrain.setPosition(0, terrainScroll);
}
public void scrollTerrain() {
if (terrainScroll < 0) {
terrainScroll += 2;
terrain.setPosition(0, terrainScroll);
}
}
// Terrain Getter
public TiledLayer getTerrain() {
return terrain;
}
// Ground/Floor color Getter
public int getGroundColor() {
return groundColor;
}
打包的代码和资源文件在:
http://www.j2medev.com/bbs/dispbbs.asp?boardid=11&id=4028&star=1#21815