Libgdx TmxMapLoader无法正常工作

时间:2015-02-14 18:36:25

标签: libgdx

TmxMapLoader让我疯了!它在一个文件中工作,而不在另一个文件中工作,它们位于相同的目录/包中,它们是相同的代码!!

ElementTowerDefense.java (工作正常!)

package com.etd;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;

public class ElementTowerDefense extends ApplicationAdapter {

private TiledMapRenderer tiledMapRenderer;
private TiledMap tiledMap;

@Override
public void create () {
    tiledMap = new TmxMapLoader().load("map/etd_1.tmx");
    tiledMapRenderer = new OrthogonalTiledMapRenderer(tiledMap);        
}

}

GameState.java (不工作!!)

package com.etd;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
public class GameState {

private TiledMap tiledMap;
private TiledMapRenderer tiledMapRenderer;  

public GameState()
{       
        tiledMap = new TmxMapLoader().load("map/etd_1.tmx");
        tiledMapRenderer = new OrthogonalTiledMapRenderer(tiledMap);    
}   
}

例外:

  

线程中的异常" main" java.lang.NullPointerException at   com.badlogic.gdx.assets.loaders.resolvers.InternalFileHandleResolver.resolve(InternalFileHandleResolver.java:26)     在   com.badlogic.gdx.assets.loaders.AssetLoader.resolve(AssetLoader.java:42)     在   com.badlogic.gdx.maps.tiled.TmxMapLoader.load(TmxMapLoader.java:91)     在   com.badlogic.gdx.maps.tiled.TmxMapLoader.load(TmxMapLoader.java:80)     在com.etd.GameState。(GameState.java:24)at   com.etd.ElementTowerDefense。(ElementTowerDefense.java:22)at   com.etd.desktop.DesktopLauncher.main(DesktopLauncher.java:12)

2 个答案:

答案 0 :(得分:0)

你能告诉你从哪里打电话给GameState吗? 例如,如果您在应用程序初始化之前调用GameState(意味着Gdx API),则可能无法使用解析器。

答案 1 :(得分:0)

我认为这就是你想要的:https://stackoverflow.com/a/25506738/1079437

基本上如Yuraj所说,你需要在构造函数中使用FileHandleResolver,如下所示:

TiledMap map = new TmxMapLoader(new ExternalFileHandleResolver()).load("map/etd_1.tmx");