We used Sprite to implement the background last time, but it is not easy to generate a map through map data. Generally, maps in the game are divided into NxN squares, and there are only several patterns. Fortunately, SUN has taken into account the features required by game developers in MIDP 2.0. TiledLayer is the layer implemented by textures, and its usage is equally simple!
First, prepare the small area we need. There are five types of map in the tank war:
They are ice, bricks, rocks, rivers, and trees. I will not say much about them.
First, read the Image: Image. createImage ("/res/img/tile.png ");
The first two Parameters specify the square number of NxN, and the last two Parameters specify the square size (not necessarily a square). We use the 8x8 size, in this way, the 40x8 png image is divided into 5 small pieces, numbered 1-5. Why not start from 0? Because 0 is used to represent transparent blocks.
Finally, use TiledLayer. setCell (col, row, index); to attach the corresponding image to each small block. If the index is 0, the small part is transparent.
OK. We changed the last spriteBackground to 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;
// The map is too large !!!
Int [] [] map = new int [] [] {// 22X22
{EMP, EMP, EMP, EMP, EMP },
{EMP, EMP, EMP, EMP, EMP },
{ROC, ROC, EMP, ICE, BRK, BRK, BRK, BRK },
{ROC, ROC, EMP, ICE, BRK, BRK, BRK, BRK },
{BRK, BRK, ROC, ROC, BRK, ROC, ROC, ROC, ROC },
{BRK, BRK, ROC, ROC, BRK, ROC, ROC, ROC, ROC },
{ROC, ROC, BRK, EMP, EMP, RIV, BRK, EMP, EMP, EMP, EMP },
{ROC, ROC, BRK, EMP, EMP, RIV, BRK, EMP, EMP, EMP, EMP },
{ROC, ROC, BRK, EMP, EMP, RIV, RIV, BRK, BRK, EMP, EMP },
{ROC, ROC, BRK, EMP, EMP, RIV, RIV, BRK, BRK, EMP, EMP },
{ROC, ROC, BRK, EMP, EMP, RIV, RIV, BRK, EMP, BRK, BRK, EMP, EMP },
{ROC, ROC, BRK, EMP, EMP, RIV, RIV, BRK, EMP, BRK, BRK, EMP, EMP },
{EMP, EMP, BRK, EMP, EMP, RIV, RIV, EMP, ROC, ROC, EMP, EMP },
{EMP, EMP, BRK, EMP, EMP, RIV, RIV, EMP, ROC, ROC, EMP, EMP },
{ICE, ICE, BRK, ROC, ROC, EMP, EMP },
{ICE, ICE, BRK, ROC, ROC, EMP, EMP },
{BRK, BRK, EMP, BRK, BRK, BRK, BRK },
{BRK, BRK, EMP, BRK, BRK, BRK, BRK },
{BRK, BRK, EMP, BRK, BRK, EMP, EMP },
{BRK, BRK, EMP, BRK, EMP, BRK, BRK, EMP, EMP },
{BRK, BRK, EMP, BRK, EMP, BRK, BRK, EMP, EMP },
{BRK, BRK, EMP, BRK, EMP, 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]);
}
}
}
...
}
...
}
The outlook is also like the way it is done. Because it is too troublesome to write a 22x22 array, I have not changed it.
Compilation and running, the effect is the same as the previous one, but with the map [] [] array, map generation and storage are very convenient, we can easily determine whether the tank can be moved (the condition is the corresponding map [x] [y] <= 1. Why do we need to put ice cubes first for convenience ), you can also easily modify the map. For example, if a brick is destroyed, you can set map [x] [y] to 0!
(To be continued...