简体   繁体   English

LibGDX:无法从资产文件夹加载json文件

[英]LibGDX: Cannot load a json file from assets folder

I have a json file with data for all the tiles in my game that I store in the assets folder. 我有一个json文件,其中包含我存储在资产文件夹中的游戏中所有图块的数据。 I try to access and parse it using TileList dataList = json.fromJson(TileList.class, Gdx.file.internal("map-elements/tiles/tiles.json")) . 我尝试使用TileList dataList = json.fromJson(TileList.class, Gdx.file.internal("map-elements/tiles/tiles.json"))访问和解析它。 This works fine for the desktop version but on the html version, after converting with gwt, I get these errors: 这对于台式机版本工作正常,但在html版本上,用gwt转换后,出现以下错误:

GwtApplication: exception: Error reading file: map-elements/tiles/tiles.json
Error reading file: map-elements/tiles/tiles.json
Couldn't find Type for class 'net.vediogames.archipelo.world.tiles.TileList'

TileList is a simple object that contains an array of TileData which can then be converted into Tile objects. TileList是一个简单的对象,其中包含一个TileData数组,然后可以将其转换为Tile对象。 I did it this way to make the json parsing easy. 我这样做是为了使json解析变得容易。

The solution to the json error is simple. json错误的解决方案很简单。 Instead of passing the FileHandle into the json parser, pass the string from the file like this: 而不是将FileHandle传递到json解析器中,而是像这样传递文件中的字符串:

TileList dataList = json.fromJson(TileList.class, Gdx.file.internal("map-elements/tiles/tiles.json").readString());

In the end, all I needed to do to solve that issue is add .readString() . 最后,解决该问题所需要做的就是add .readString() As for the Couldn't find Type for class 'net.vediogames.archipelo.world.tiles.TileList' error, I also found a solution but it is more complicated. 至于Couldn't find Type for class 'net.vediogames.archipelo.world.tiles.TileList'错误,我也找到了一个解决方案,但是它更复杂。

JavaScript handles class references differently than Java. JavaScript处理类引用的方式与Java处理方式不同。 So I was not able to use TileList.class without first registering it so LibGDX can generate a Reflection. 因此,如果不先注册,就无法使用TileList.class ,这样LibGDX可以生成反射。 What I needed to do was add this line into my *.gwt.xml files: 我需要做的是将此行添加到我的* .gwt.xml文件中:

<extend-configuration-property name="gdx.reflect.include" value="net.vediogames.archipelo.world.tiles.TileList" />

If you want a full tutorial about how reflection works and how to include packages or exclude, please view the official LibGDX tutorial here . 如果您需要有关反射如何工作以及如何包含软件包或排除软件包的完整教程,请在此处查看官方的LibGDX教程。

Your solution was not working for me, I still got the same error. 您的解决方案对我不起作用,我仍然遇到相同的错误。 After some hours of testing I got it to work, using your suggestions and by using the ClassReflection instead of referencing the class itself. 经过几个小时的测试,我使用您的建议并使用ClassReflection而不是引用类本身来使其工作。 Your example: 你的例子:

TileList dataList = json.fromJson(TileList.class, Gdx.file.internal("map-elements/tiles/tiles.json").readString());

looks in my working code like: 在我的工作代码中看起来像:

TileList dataList = (TileList) json.fromJson(ClassReflection.forName(TileList.class.getName()), Gdx.file.internal("map-elements/tiles/tiles.json").readString());

This is quite a pain in the a.. but I'm glad it is finally working now. 这是一个很大的痛苦。但是我很高兴它现在终于可以工作了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM