出于某种原因,我为Map创建了一些类,用pyxeledit创建了它,但是当我运行它时,除了在一个空白窗口中的角色之外,我什么也得不到。我不知道为什么我不能绘制Map。
这就是代码,我真的不明白为什么它不读取xml文件。
public class TileManager { public static ArrayList tm;
public TileManager()
{
tm=new ArrayList<TileMap>();
}
public TileManager(String path)
{
tm=new ArrayList<TileMap>();
addTileMap(path,64,64);
}
private void addTileMap(String path, int blockWidth, int blockHeight)
{
String imagePath;
int width=0;
int height=0;
int tileWidth;
int tileHeight;
int tileCount;
int tileColumns;
int layers=0;
Sprite sprite;
String[] data=new String[10];
try{
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder=builderFactory.newDocumentBuilder();
Document doc= builder.parse(new File(getClass().getClassLoader().getResource(path).toURI()));
doc.getDocumentElement().normalize();
NodeList list=doc.getElementsByTagName("tileset");
Node node = list.item(0);
Element eElement = (Element) node;
imagePath=eElement.getAttribute("name");
tileWidth=Integer.parseInt(eElement.getAttribute("tilewidth"));
tileHeight=Integer.parseInt(eElement.getAttribute("tileheight"));
tileCount=Integer.parseInt(eElement.getAttribute("tilecount"));
tileColumns=Integer.parseInt(eElement.getAttribute("columns"));
sprite=new Sprite("tile/"+imagePath+".png", tileWidth,tileHeight);
list=doc.getElementsByTagName("layer");
layers=list.getLength();
for(int i=0;i<layers;i++)
{
node=list.item(i);
eElement=(Element) node;
if(i<=0)
{
width=Integer.parseInt(eElement.getAttribute("width"));
height=Integer.parseInt(eElement.getAttribute("height"));
}
data[i]=eElement.getElementsByTagName("data").item(0).getTextContent();
if(i>= 1)
{
tm.add(new TileMapNorm(data[i],sprite,width,height,blockWidth,blockHeight,tileColumns));
}
else
{
tm.add(new TileMapObj(data[i],sprite,width,height,blockWidth,blockHeight,tileColumns));
}
}
} catch(Exception e)
{
System.out.println("EROOR - TILEMANAGER: can not read tilemap");
}
}
public void render(Graphics2D g)
{
for(int i=0; i<tm.size(); ++i)
{
tm.get(i).render(g);
}
}
}
这也是我要读的xml文件
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.5" tiledversion="1.5.0" orientation="orthogonal" renderorder="right-down" width="50" height="50" tilewidth="16" tileheight="16" infinite="0" nextlayerid="3" nextobjectid="1">
<tileset firstgid="1" name="0x72_16x16DungeonTileset_walls.v1.tmx" tilewidth="16" tileheight="16" tilecount="625" columns="25">
<image source="../../Facultate An2/Java/ProiectJava/0x72_16x16DungeonTileset_walls.v1.tmx.png" width="400" height="400"/>
</tileset>
<layer id="2" name="Tile Layer 2" width="50" height="50">
<data encoding="csv">
暂无答案!
目前还没有任何答案,快来回答吧!