简体   繁体   中英

LibGDX: Can't manage to render a TiledMap with viewport

Hello :)

Recently I have started programming in LibGDX. Now im 4 days stuck in rendering TiledMap on Viewport. I cant figure it out, but i know it is possible. Here is how the testing TiledMap looks:

Testing tiled map

How the game looks now ( no tiles showing ):

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

And here is my code in 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;
    }
}

The LevelManager.tiledMap gets loaded in create method in MainGame class, so I load it just fine, but it doesnt show on the screen. It does when i dont use the Viewport.

Thank you all for future help :)

我发现了这个问题,这是因为MainGame类中的PPM(每米像素数),采用Integer格式且必须为Float ,否则地图将不会因为比例缩放中的划分问题而呈现。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM