1

所以我正在用 TileManager 编写基本的 Java 2D 游戏。我正在 Tiled 中创建地图并导出 .xml 文件。

这里有一个例子:

<?xml version="1.0" encoding="UTF-8"?>
<map version="1.5" tiledversion="1.7.2" orientation="orthogonal" renderorder="right-down" width="25" height="20" tilewidth="16" tileheight="16" infinite="0" nextlayerid="2" nextobjectid="1">
 <tileset firstgid="1" name="outdoor" tilewidth="16" tileheight="16" tilecount="1975" columns="25">
  <image source="outdoor.png" trans="ffffff" width="400" height="1264"/>
 </tileset>
 <layer id="1" name="Back" width="25" height="20">
  <data encoding="csv">
176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,
176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,
176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,
176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,
176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,
176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,
176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,
176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,
176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,
176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,
176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,
176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,
176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,
176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,
176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,
176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,
176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,
176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,
176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,
176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176
</data>
 </layer>
</map>

以下代码获取信息并呈现特定图块:

public class TileMapNorm extends TileMap{
    
    private ArrayList<Block> blocks;
    
    public TileMapNorm(String data, Sprite sprite, int width, int height, int tileWidth, int tileHeight, int tileCols) {
        blocks = new ArrayList<Block>();
        String[] block = data.split(",");
        for(int x = 0; x < (width * height); x++) {
            int tmp = Integer.parseInt(block[x].replaceAll("\\s+", ""));
            if(tmp != 0) {
                blocks.add(new NormBlock(sprite.getSprite((int) ((tmp - 1) % tileCols), (int) ((tmp - 1) / tileCols)), new Vector2((int) (x % width) * (tileWidth), (int) (x / height) * (tileHeight)), tileWidth, tileHeight));
            }
            
        }
    }
    
    public void render(Graphics2D g) {
        for(int x = 0; x < blocks.size(); x++) {
            blocks.get(x).render(g);
        }
    }
}

当地图的宽度和高度相等或高度大于宽度时,地图渲染得很好。但是当宽度大于高度时,就会出现如下渲染问题:

渲染错误的图像

问题是什么,如何解决?

4

0 回答 0