简体   繁体   English

LibGDX-如何在屏幕上将等轴测平铺地图居中?

[英]LibGDX - How to center an Isometric Tiled Map on screen?

I've been tinkering around with LibGDX for a few days now. 我一直在和LibGDX纠缠几天。 I've managed to render an Isometric Tiled Map onto the screen but I just can't seem to figure out how to center it properly. 我设法在屏幕上渲染了一个等距平铺贴图,但似乎无法弄清楚如何正确居中。 Here's the code: 这是代码:

public class PlayScreen implements Screen {

    private TiledMap map;
    private IsometricTiledMapRenderer isometricRenderer;
    private OrthographicCamera camera;

    public void createUI() {
        // Load map from tiled .tmx file.
        map = new TmxMapLoader().load("arena1/arena1.tmx");

        // Setup isometric renderer and camera.
        isometricRenderer = new IsometricTiledMapRenderer(map);
        camera = new OrthographicCamera();
    }

    @Override
    public void show() {
        Gdx.gl.glClearColor(0, 0, 1, 1);
        createUI();
    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        isometricRenderer.setView(camera);
        isometricRenderer.render();
    }

    @Override
    public void resize(int width, int height) {
        camera.viewportWidth = width;
        camera.viewportHeight = height;
        camera.update();
    }

    @Override
    public void pause() {}

    @Override
    public void resume() {}

    @Override
    public void hide() {}

    @Override
    public void dispose() {
        map.dispose();
        isometricRenderer.dispose();
    }

}

And here is what I get: 这就是我得到的:

在此处输入图片说明

Thank you in advance! 先感谢您!

Tiled map hasn't setPosition method or something like this. 平铺地图没有setPosition方法或类似方法。 So what you should do is move your camera. 因此,您应该做的就是移动相机。

Call this in your render method to move your camera by WASD: 在渲染方法中调用此命令,以通过WASD移动相机:

private void cameraController(Camera camera){

    if (Gdx.input.isKeyPressed(Input.Keys.W)) {
       camera.translate(0, 10, 0);
    }
    if (Gdx.input.isKeyPressed(Input.Keys.S)) {
       camera.translate(0, -10, 0);
    }
    if (Gdx.input.isKeyPressed(Input.Keys.A)) {
       camera.translate(-10, 0, 0);
    }
    if (Gdx.input.isKeyPressed(Input.Keys.D)) {
       camera.translate(10, 0, 0);
    }
}

You can also change 10 to speed which you want. 您也可以将10更改为所需的速度。

Or if you don't want to move it by WASD you can just set position directly. 或者,如果您不想通过WASD移动它,则可以直接设置位置。

camera.setPosition(x, y, z); 

Managed to do it with 设法做到这一点

camera.position.set(768, 0, 0);

768 is half the width of the map. 768是地图宽度的一半。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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