How to set map size on Android using LibGdx

大兔子大兔子 提交于 2019-12-31 05:22:30

问题


I'm working on a simple game and I have problem with setting the map size. I am using OrthographicCamera. I want the map to be visible. How should I set the proper size of viewport new OrthographicCamera(viewport_width, viewport_height)? Currently I'm passing some value and when I'm drawing elements on the map I don't see them because they are out of bound, if I make the width and height enormous I can see them. I want to have the whole map visible and draw everything on the sight of the user. I'm not sure where I'm making a mistake


回答1:


You map is in square size and most of devices are in rectangular size so you need to keep your map in center of screen, either you're using portrait or landscape mode.

public class TileTest extends ApplicationAdapter {

    ExtendViewport extendViewport;
    OrthogonalTiledMapRenderer mapRenderer;
    OrthographicCamera camera;
    float worldWidth,worldHeight;

    @Override
    public void create() {

        float tileWidth=64,tileHeight=64;
        float mapWidth=20,mapHeight=20;

        worldWidth=tileWidth*mapWidth;
        worldHeight=tileHeight*mapHeight;

        camera=new OrthographicCamera();
        extendViewport =new ExtendViewport(worldWidth,worldHeight,camera);

        TmxMapLoader mapLoader=new TmxMapLoader();
        TiledMap map=mapLoader.load("square.tmx");

        mapRenderer=new OrthogonalTiledMapRenderer(map);
    }

    @Override
    public void render() {

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

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

    @Override
    public void dispose() {
        mapRenderer.dispose();
    }

    @Override
    public void resize(int width, int height) {
        extendViewport.update(width,height,false);
        extendViewport.getCamera().position.set(worldWidth/2,worldHeight/2,0);
        extendViewport.getCamera().update();
   }
}

Output is :



来源:https://stackoverflow.com/questions/45018391/how-to-set-map-size-on-android-using-libgdx

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!