Libgdx - 未呈现TMX地图

时间:2016-07-30 12:47:57

标签: java android libgdx

我正在制作2D Android游戏。问题是我的平铺游戏地图没有渲染,虽然我的GUI类很好。看起来永远不会调用renderer.render()

这是我手机的屏幕截图。

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();
    }
}

1 个答案:

答案 0 :(得分:0)

我认为你应该在create()中使用 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);

试试看,让我知道。感谢...