我一直在研究磁贴集和Java的地图编辑。但是,我遇到了一个问题。
映射文件指定要放置的图块,如下所示:
tileset image: tiles.png
tileset width: 1024
tileset height: 1024
tileset tile width: 16
tileset tile heigth: 16
tiles on x axis: 5
tiles on y axis: 6
tiles: 1,1,1,1,1,1,1,1,1,1,1,1,3,2,2,2,2,2,22,3,4,26,21,9,19,28,18,2,1,1,1,1 etc...
如图所示,将读取图块图像:
X:
1 - 2 - 3 - 4 - 5
-----------------------
1| 1 | 2 | 3 | 4 | 5
2| 6 | 7 | 8 | 8 | 10
Y:3| 11| 12| 13| 14| 15
4| 16| 17| 18| z | 20
5| 21| 22| 23| 24| 25
6| 26| 27| 28| 29| 30
('|仅用于间隔)
为了帮助理解此表,在“ z”处的图块集x,y坐标为(4,4),其索引为19。
现在忘记了图像,因为它们在这里无关紧要,我想知道如何从倾斜集(z的(4,4))上的坐标获取索引(z的19)。
例如如果我有此代码:
int tilesetTilesWidth = tilesetImageWidth / tileWidth;
int tilesetTilesHeight = tilesetImageHeight / tileHeight;
int[][] coords = new int[tilesetTilesWidth][tilesetTilesHeight];
int[] indexes = new int[tilesetTilesWidth * tilesetTilesHeight];
for(int x = 0; x < coords.length; x++){
for(int y = 0; y < coords[x].length; y++){
indexes[?] = coords[x][y]; //What should ? or this line be replaced with?
}
}
什么应该 ?还是用这行代替?
注意:
如果我有任何疑问或想念的东西,请立即发表评论。
最佳答案
安德森的答案可能是您正在寻找的东西,但是为了回答这个问题,我相信索引应该是(y-1)*cords.length+x
。
请注意,在此示例中,由于cords.length = 5,y = 4和x = 4,我们将根据需要获得(4-1)* 5 + 4 = 3 * 5 + 4 = 15 + 4 = 19。