在 Android Studio 中解析 tmx 和 JSON 文件时出错

Error Parsing tmx and JSON file in Android Studio

我正在尝试在我的 Android 工作室项目中加载由 Tiled 制作的简单地图。 我正在使用 libGDX 制作一个简单的平台游戏。 在 Tiled 中,我刚刚导入了一个 tileset,创建了一个平台和其他任何东西。

现在我想在 部分了解我上传失败是因为我无法理解为什么会出现此错误:

Exception in thread "LWJGL Application" com.badlogic.gdx.utils.SerializationException: Error parsing file: mapassets.json
at com.badlogic.gdx.utils.XmlReader.parse(XmlReader.java:83)
at com.badlogic.gdx.maps.tiled.TmxMapLoader.getDependencyFileHandles(TmxMapLoader.java:116)
at com.badlogic.gdx.maps.tiled.TmxMapLoader.load(TmxMapLoader.java:74)
at com.badlogic.gdx.maps.tiled.TmxMapLoader.load(TmxMapLoader.java:59)
at com.arcadan.push_the_player.TiledTest.create(TiledTest.java:28)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:150)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.run(LwjglApplication.java:127)


Caused by: com.badlogic.gdx.utils.SerializationException: Error parsing XML on line 1 near: 
{ "columns":10,
 "image":"..\/..
at com.badlogic.gdx.utils.XmlReader.parse(XmlReader.java:330)
at com.badlogic.gdx.utils.XmlReader.parse(XmlReader.java:61)
at com.badlogic.gdx.utils.XmlReader.parse(XmlReader.java:81)
... 6 more

这里是地图 .tmx :

<?xml version="1.0" encoding="UTF-8"?>
<map version="1.4" tiledversion="1.4.3" orientation="orthogonal" renderorder="right-down" width="20" height="20" tilewidth="12" tileheight="12" infinite="0" nextlayerid="2" nextobjectid="1">
 <editorsettings>
  <export target="../../android/assets/MyMap.tmx" format="tmx"/>
 </editorsettings>
 <tileset firstgid="1" source="../mapassets.json"/>
 <layer id="1" name="Tile Layer 1" width="20" height="20">
  <data encoding="csv">
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,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,2,3,4,5,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,11,12,13,14,15,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,21,22,23,24,25,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,31,32,33,34,35,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,41,42,43,44,45,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,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,0,0,0,0,0,0,0,0,0,0,0,0,0,0
</data>
 </layer>
</map>

这是令我沮丧的 mapassets.json(此文件位于我项目的 assets/mapAssets 文件夹中):

{ "columns":10,
 "image":"..\/..\/assets\/mapAssets\/mapassets.png",
 "imageheight":128,
 "imagewidth":128,
 "margin":0,
 "name":"mapassets",
 "spacing":0,
 "tilecount":100,
 "tiledversion":"1.4.3",
 "tileheight":12,
 "tilewidth":12,
 "type":"tileset",
 "version":1.4
}

这里是我加载 .tmx 文件的方式:

    @Override
public void create () {
    float w = Gdx.graphics.getWidth();
    float h = Gdx.graphics.getHeight();
    camera = new OrthographicCamera();
    camera.setToOrtho(false,w,h);
    camera.update();
    tiledMap = new TmxMapLoader().load("MyMap.tmx");
    tiledMapRenderer = new OrthogonalTiledMapRenderer(tiledMap);
    Gdx.input.setInputProcessor(this);
}

您收到错误是因为您已将图块集保存为 .json 文件,但 TmxMapLoader 中的 libGDX 实现假定它是在 XML.

您可以在 source code for TmxMapLoader.

中看到

将您的图块集另存为 .tsx 文件并从您的 .tmx 文件中引用它,它应该可以工作。