I’ve been trying to find some deep and complete explanations on Backgroung tiling maps for weeks, but I still can’t be confident at it. I need help.
I have my GamePanel class with the gameloop and the graphics g used to paint things on the screen. Quite well, my character is moving around and jumping happily.
Now comes the hard part, the TileMap class.
First question: which variables shall I use?
For now I’m thinking about those
//coordinates that I may use to offset the screen
public int Tilemapx;
public int Tilemapy;
//number of tiles in the image
private int TotalRows;
private int TotalColumns;
private int Rows;
private int Cols;
//size of the Tile
public int TileSize;
//Two Buffered images, one for the entire map and one for the single tile
public BufferedImage EntireMap;
public BufferedImage Tile;
//a two dimensional array so I can reach any tile with two coordinates
public int [] [] map;
Going on step by step I’d make a contructor and put on TotalRows and TotalColumns the columns and Rows to be rendered(+2 offscreen)
public TileMap (int TileSize){
this.TileSize = TileSize;
TotalRows = Pannello.WIDTH / TileSize + 2;
TotalColumns = Pannello.HEIGHT / TileSize + 2;
}
So far so good(I hope).
Now, everybody speaks about this for loop, and I imagine that I have to retrieve a tile (or tiles?) with the subimage
and for the sake of learning I would be happy to display just one tile(the text map that drives the graphics is something I want to study when I’m more prepared).
So, here I have a method to load the map that I painted in Photoshop and the for loop that is supposed to draw a tile on 0, 0.
public void loadTileImg (){
String TileMapPath = "res/Tiles.png";
try {
EntireMap = ImageIO.read(new File(TileMapPath));
} catch (IOException e) {
e.printStackTrace();
}
for (int Cols = 0; Cols < TotalColumns; Cols++){
for (int Rows = 0; Rows < TotalRows; Rows++){
Tile = EntireMap.getSubimage(0, 0, TileSize, TileSize);
}
That’s where my lacking of experience and newbieness comes in. The screen is actually flashing like a strobo and the tile isn’t getting displayed.