Java Reference
In-Depth Information
A TiledLayer 's constructor takes everything necessary to create the TiledLayer image
except the array of cell indexes, which you pass to its setCell method. Thus, to create a
TiledLayer , you provide the following:
• The number of columns and rows in the tile image
• The image of tiles
• The width of a single tile in the image of tiles
• The height of a single tile in the image of tiles
For example, to create the TiledLayer used to draw the image in Figure 8-2(c), you
would write something like the code shown in Listing 8-5.
Listing 8-5. Creating the TiledLayer
Image boardImage = Image.createImage(imageName);
private final int tileWidth = 16, tileHeight = 16;
private final int cellsWidth = 8, cellsHeight = 8;
board = new TiledLayer( cellsWidth, cellsHeight, boardImage, tileWidth, tileHeight);
Once you create the TiledLayer , you need to set the index of the image of each cell in
the TiledLayer to the index of the tile in the tile bitmap. You do this using the setCell
method, passing the coordinates of the cell you want to set and the index of the tile in the
tile image, as shown in Listing 8-6.
Listing 8-6. Using the setCell Method
int[] map = {
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 2, 1, 1, 1, 1, 1,
1, 1, 1, 1, 3, 1, 1, 1,
1, 2, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 2, 1,
1, 1, 1, 3, 1, 1, 1, 1,
1, 1, 1, 1, 1, 3, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1
};
for( int i = 0; i < map.length; i++ ) {
int x = i % cellsWidth;
int y = i / cellsHeight;
board.setCell(x, y, map[i]);
}
 
Search WWH ::




Custom Search