Question

I want to know how to make tile based map in java. I want to make a basic 2d platformer type or top-down type.I have already researched, but none helped much. However, what I have learned is that I have to make tile objects and a two-dimensional matrix to store position of tiles.

Also there are many programs available on the internet for making maps. i do not want to use them.

Was it helpful?

Solution

You could use a two dimensional array to store your map tiles like so,

int[][] map =
{
{1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
};

Then access your map positions like map[1][1]. Or change the variables like map[0][0] = 1.

Instead of writing out an entire array like that, you may prefer to use a loop to generate your 0's, or 1's or whatever it is you want your tiles to be.

like so,

int[][] tileMap = new int[10][10];

for(int i = 0; i < tileMap.length; i++) {
    for(int j = 0; j < tileMap[0].length; j++) {
       tileMap[i][j] = 0;//put whatever number you want in 
       //here and it will insert it instead of 0's
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top