2014-12-02 65 views
0

我看過類似這樣的問題,但沒有一個能夠與我的Libgdx項目(僅適用於桌面)相匹配。我使用Tiled製作了一個.tmx貼圖,並將其複製到assets文件夾中,該文件夾有兩個子包:gameScreens(包含貼圖)和貼圖。如果我使用Tiled從Eclipse打開.tmx映射,它工作得很好。這裏的.tmx:NullPointerException Libgdx

<?xml version="1.0" encoding="UTF-8"?> 
<map version="1.0" orientation="orthogonal" renderorder="right-down" width="8" height="8" tilewidth="32" tileheight="32"> 
<tileset firstgid="1" name="default" tilewidth="32" tileheight="32"> 
    <tile id="0"> 
    <image width="32" height="32" source="../tiles/rock.png"/> 
    </tile> 
    <tile id="1"> 
    <image width="32" height="32" source="../tiles/tile_default_1.jpg"/> 
    </tile> 
    <tile id="2"> 
    <image width="32" height="32" source="../tiles/tile_default_2.jpg"/> 
    </tile> 
    <tile id="3"> 
    <image width="32" height="32" source="../tiles/wall.png"/> 
    </tile> 
</tileset> 
<layer name="Camada de Tiles 1" width="8" height="8"> 
    <data encoding="base64" compression="gzip"> 
    eJxjYWBgYCGAmXFgZHlGJHFGLPKU8okxH5f78GEAbrAA2Q== 
    </data> 
</layer> 
</map> 

我創建了一個抽象類遊戲狀態:

package com.mygdx.cavedive.game.GameStates; 

//imports 

public abstract class GameState { 

    private static TmxMapLoader mapLoader = new TmxMapLoader(); 
    protected static OrthogonalTiledMapRenderer otmr; 
    protected static OrthographicCamera cam; 

    protected OrthogonalTiledMapRenderer getRenderer() { 

     return otmr; 

    } 

    protected TmxMapLoader getMapLoader() { 

     return mapLoader; 

    } 

    protected void disposeRenderer() { 

     otmr.dispose(); 

    } 

} 

和擴展一個類是:

package com.mygdx.cavedive.game.GameStates; 

//imports 

public class Level_1 extends GameState implements Screen { 

    private TiledMap map; 

    @Override 
    public void render(float delta) { 

     //Clear the screen 
     Gdx.gl.glClearColor(0, 0, 0, 1); 
     Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 

     otmr.setView(cam); 

     //Render the screen 
     otmr.render(); 

    } 

    @Override 
    public void resize(int width, int height) { 

     cam.viewportWidth = width; 
     cam.viewportHeight = height; 
     cam.update(); 

    } 

    @Override 
    public void show() { 

     map = getMapLoader().load("gameScreens/level1.tmx"); 
     otmr = new OrthogonalTiledMapRenderer(map, 1f/32f); 
     cam = new OrthographicCamera(); 

    } 

    @Override 
    public void hide() { 

     dispose(); 

    } 

    @Override 
    public void pause() { 
    } 

    @Override 
    public void resume() { 
    } 

    @Override 
    public void dispose() { 

     disposeRenderer(); 
     map.dispose(); 

    } 

} 

現在,這裏就是我得到:

Exception in thread "LWJGL Application" java.lang.NullPointerException 
    at com.badlogic.gdx.maps.tiled.TmxMapLoader.loadTilesets(TmxMapLoader.java:246) 
    at com.badlogic.gdx.maps.tiled.TmxMapLoader.load(TmxMapLoader.java:118) 
    at com.badlogic.gdx.maps.tiled.TmxMapLoader.load(TmxMapLoader.java:104) 
    at com.mygdx.cavedive.game.GameStates.Level_1.show(Level_1.java:40) 
    at com.badlogic.gdx.Game.setScreen(Game.java:61) 
    at com.mygdx.cavedive.game.App.GameCore.create(GameCore.java:28) 
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:136) 
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114) 

它與瓷磚,但我無法弄清楚什麼。

+0

我剛剛發現發生此問題是因爲我將單獨的圖像加載到了我的tileset。然而,將它們一起加入到一個.png中,使其正常運行。我仍然不知道它爲什麼這樣工作。 – 2014-12-02 01:40:23

回答

1

其中每個圖塊引用其自己的圖像的「圖像集合」tileset是Tiled 0.10中的一項新功能。很可能您正在使用的libgdx版本不支持這些類型的tilesets。根據this issue on github,libgdx nightlies支持這個功能。

+0

哦,就是這樣。謝謝 – 2014-12-03 23:24:55

+0

很高興幫助!你還可以將我的答案標記爲接受嗎? – 2014-12-05 08:38:37