2016-07-30 166 views
0

我正在製作2D Android遊戲。問題是我的平鋪遊戲地圖沒有渲染,雖然我的GUI類很好。它看起來像renderer.render()永遠不會被調用。Libgdx - 未呈現TMX地圖

這是從我的手機屏幕拍攝。

enter image description here

應該使這個平鋪圖,但事實並非如此。 enter image description here

Main.class:

public class Main extends ApplicationAdapter implements Screen { 
    private ZombieGame game; 
    private OrthographicCamera camera; 
    private Viewport gameView; 
    private GUI gui; 
    private TmxMapLoader mapLoader; 
    private TiledMap map; 
    private OrthogonalTiledMapRenderer renderer; 

    public Main(ZombieGame game){ 
     this.game = game; 

     camera = new OrthographicCamera(); 
     gameView = new FitViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), camera); 

     gui = new GUI(game.batch); 

     mapLoader = new TmxMapLoader(); 
     map = mapLoader.load("level1.tmx"); 
     renderer = new OrthogonalTiledMapRenderer(map); 
     camera.position.set(gameView.getWorldWidth()/2, gameView.getWorldHeight()/2, 0); 
    } 

    @Override 
    public void show(){ 

    } 

    @Override 
    public void hide(){ 

    } 

    @Override 
    public void create() { 

    } 

    public void keyEvent(float dt){ 
     if(Gdx.input.isTouched()){ 
      camera.position.x -= 100 * dt; 
      System.out.println("COORD: " + camera.position.x); 
     } 
    } 

    public void update(float dt){ 
     keyEvent(dt); 
     camera.update(); 
     renderer.setView(camera); 
    } 

    @Override 
    public void render(float delta) { 
     update(delta); 

     Gdx.gl.glClearColor(0, 0, 0, 1); 
     Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 

     renderer.render(); 

     game.batch.setProjectionMatrix(gui.stage.getCamera().combined); 
     gui.stage.draw(); 
    } 

    @Override 
    public void dispose() { 

    } 
} 

ZombieGame.class:

public class ZombieGame extends Game{ 
    public SpriteBatch batch; 

    @Override 
    public void create(){ 
     batch = new SpriteBatch(); 
     setScreen(new Main(this)); 
    } 

    @Override 
    public void render(){ 
     super.render(); 
    } 
} 
+0

你可以添加一些細節,以程序是應該做什麼? – Shawn

+0

我添加了該平鋪地圖的圖像。 – iUuax

+0

修正錯字和輕微機械問題。 – Prune

回答

0

我認爲你應該在使用setToOrtho()方法創建(),

camera.setToOrtho(false,Gdx.graphics.getWidth(),Gdx.graphics.getHeight()); 

代替FitViewport,

gameView = new FitViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), camera); 
camera.position.set(gameView.getWorldWidth()/2, gameView.getWorldHeight()/2, 0); 

只是試一試,讓我知道。謝謝...