我觉得这个主意不错。如果tilesize改变了bordersize也会改变。您不能在其周围创建不同大小的图块作为框架。如果您想始终创建相同的框架,您可以使用简单的Ninepatch 和地图大小。
创建这样的东西:
NinePatch p = new NinePatch(texture, 5, 5, 5, 5); // the edge sizes are 5px here.
在 render 方法中,您以 tilemap 的大小绘制它。您可以从 Map 对象中选择尺寸:
p.draw(batch, 0, 0, tilesize*tilecountx, tilesize*tilecounty);
如果你不喜欢他的想法,你可以选择第二个版本。创建一个新的TiledMap,将边界作为图块,您需要复制整个图块地图,偏移量为 1 个图块(因为您需要有 1 个图块空间)并创建一个包含 2 行和多列的新图块图。
应该看起来像这样:
map = new TiledMap(); // the map with the bordercells
MapLayers layers = map.getLayers();
for (int l = 0; l < map1.getLayers().getCount(); l++) {
// create a new layer which is bigger
TiledMapTileLayer newlayer = new TiledMapTileLayer(
((TiledMapTileLayer) map1.getLayers().get(0)).getWidth() + 2,
((TiledMapTileLayer) map1.getLayers().get(0)).getHeight() + 2,
(int) ((TiledMapTileLayer) map1.getLayers().get(0))
.getTileWidth(), (int) ((TiledMapTileLayer) map1
.getLayers().get(0)).getTileHeight());
// now add the cells here
for (int x = 0; x < newlayer.getWidth(); x++) {
for (int y = 0; y < newlayer.getHeight(); y++) {
//add the right tile regions here!
if(x == 0 && y == 0){
Cell cell = new Cell();
cell.setTile(new StaticTiledMapTile(first edge));
newlayer.setCell(x, y, cell);
continue;
}
if(x == newlayer.getWidth()-1 && y == 0){
Cell cell = new Cell();
cell.setTile(new StaticTiledMapTile(second edge));
newlayer.setCell(x, y, cell);
continue;
}
if(x == 0 && y == newlayer.getHeight()-1){
Cell cell = new Cell();
cell.setTile(new StaticTiledMapTile(third edge));
newlayer.setCell(x, y, cell);
continue;
}
if(x == newlayer.getWidth()-1 && y == newlayer.getHeight()-1){
Cell cell = new Cell();
cell.setTile(new StaticTiledMapTile(fourth edge));
newlayer.setCell(x, y, cell);
continue;
}
if(x == 0){
Cell cell = new Cell();
cell.setTile(new StaticTiledMapTile(textureregion for bottom here));
newlayer.setCell(x, y, cell);
continue;
}
if(y == 0){
Cell cell = new Cell();
cell.setTile(new StaticTiledMapTile(textureregion for first col));
newlayer.setCell(x, y, cell);
continue;
}
if(x == newlayer.getWidth()-1){
Cell cell = new Cell();
cell.setTile(new StaticTiledMapTile(textureregion for toprow));
newlayer.setCell(x, y, cell);
continue;
}
if(y == newlayer.getHeight()-1){
Cell cell = new Cell();
cell.setTile(new StaticTiledMapTile(textureregion for last col));
newlayer.setCell(x, y, cell);
continue;
}
//last but not least if none of this fit add the tile from the old map
//dont forget the offsett
newlayer.setCell(x, y, ((TiledMapTileLayer)map1.getLayers().get(l)).getCell(x-1, y-1)); //map1 is the original map without the borders
}
}
layers.add(newlayer);
}
如果你真的有不同的瓦片大小,我会为你拥有的每个瓦片大小添加一个 Bordertexture,以防你使用 TiledMap 版本。如果您想调整Texture 的大小,请使用Sprite。 Sprite 提供.setSize(w,h) 并扩展了TextureRegion。要创建静态TiledMapTile,您还可以将Sprite 添加为Texture。 因此,如果您接受这个想法,请在创建 StaticTiledMapTile 之前使用 Sprite 并将其调整为平铺大小。