LibGDX:无法使用视口渲染TiledMap

时间:2016-07-09 10:07:28

标签: java android libgdx rendering tiled

您好:)

最近我开始在LibGDX中编程。现在我在视口上渲染TiledMap 4天了。我无法弄明白,但我知道这是可能的。以下是测试TiledMap的外观:

Testing tiled map

游戏现在的样子(没有瓷砖显示):

The PlayScreen now (No tiles showing) Screen is blue becouse of ClearColor code down below.

这是我在PlayScreen中的代码:

    public class PlayScreen implements Screen, InputProcessor{
    MainGame game;

    private OrthographicCamera camera;
    private Viewport gamePort;

    private OrthogonalTiledMapRenderer renderer;


    public PlayScreen(MainGame game) {
        this.game = game;

        camera = new OrthographicCamera();
        gamePort = new StretchViewport(MainGame.V_WIDTH / MainGame.PPM,
                MainGame.V_HEIGHT / MainGame.PPM, camera);
        renderer = new OrthogonalTiledMapRenderer(LevelManager.tiledMap,
                1/ MainGame.PPM);
        camera.position.set(gamePort.getWorldWidth() / 2, gamePort.getWorldHeight() / 2, 0);

        Gdx.input.setInputProcessor(this);
    }

    @Override
    public void show() {

    }

    public void update(float delta){
        camera.update();
    }

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

        Gdx.gl.glClearColor((float)66/255, (float)66/255, (float)255/255, 0f);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        renderer.setView(camera);
        renderer.render();
    }

    @Override
    public void resize(int width, int height) {
        gamePort.update(width, height);
    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void hide() {

    }

    @Override
    public void dispose() {

    }

    @Override
    public boolean keyDown(int keycode) {
        return false;
    }

    @Override
    public boolean keyUp(int keycode) {
        return false;
    }

    @Override
    public boolean keyTyped(char character) {
        return false;
    }

    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        return false;
    }

    @Override
    public boolean touchUp(int screenX, int screenY, int pointer, int button) {
        return false;
    }

    @Override
    public boolean touchDragged(int screenX, int screenY, int pointer) {
        return false;
    }

    @Override
    public boolean mouseMoved(int screenX, int screenY) {
        return false;
    }

    @Override
    public boolean scrolled(int amount) {
        return false;
    }
}

LevelManager.tiledMap在MainGame类的create方法中加载,所以我加载它很好,但它没有在屏幕上显示。当我不使用视口时它会这样做。

谢谢大家以后的帮助:)

1 个答案:

答案 0 :(得分:0)

我解决了这个问题,因为它是MainMame类中的PPM(每米像素数),格式为整数,它必须是 Float ,否则由于缩放中的除法问题,地图不会呈现。