Java Reference
In-Depth Information
The col , row , numCols , and numRows parameters describe a rectangular region of cells that
will be filled with the specified tile. For example, fillCells(2, 0, 1, 2, 6) would assign tile 6
to the cells in the first and second rows of the third column of the tiled layer.
The following excerpt (adapted from QuatschCanvas.java in the source code) demonstrates
one way to create and initialize a TiledLayer :
Image backgroundImage = Image.createImage("/background_tiles.png");
TiledLayer background = new TiledLayer(8, 4, backgroundImage, 48, 48);
background.setPosition(12, 0);
int[] map = {
1, 2, 0, 0, 0, 0, 0, 0,
3, 3, 2, 0, 0, 0, 5, 0,
3, 3, 3, 2, 4, 1, 3, 2,
6, 6, 6, 6, 6, 6, 6, 6
};
for (int i = 0; i < map.length; i++) {
int column = i % 8;
int row = (i - column) / 8;
background.setCell(column, row, map[i]);
}
Using the source image of Figure 14-1, this code produces the tiled layer shown in
Figure 14-4.
Figure 14-4. A tiled layer
You now know almost everything there is to know about TiledLayer ; it serves as a simple
map between a palette of tiles and a fully assembled layer.
Using Animated Tiles
There is one additional twist: animated tiles. An animated tile is a virtual tile whose mapping
can be changed at runtime. Although you could accomplish the same thing by calling setCell()
on all the cells you wanted to change, using an animated tile allows you to make a single call
that changes all the affected cells.
To use an animated tile, you create one by calling this method:
public int createAnimatedTile(int staticTileIndex)
 
Search WWH ::




Custom Search