如何使用 LibGdx 在 Android 上设置地图大小 [英] How to set map size on Android using LibGdx

查看:27
本文介绍了如何使用 LibGdx 在 Android 上设置地图大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个简单的游戏,但在设置地图大小时遇到​​了问题.我正在使用 OrthographicCamera.我希望地图可见.我应该如何设置合适的视口尺寸new OrthographicCamera(viewport_width, viewport_height)?目前我正在传递一些价值,当我在地图上绘制元素时,我看不到它们,因为它们超出了界限,如果我将宽度和高度设置得很大,我可以看到它们.我想让整个地图可见,并在用户的视线范围内绘制所有内容.我不确定我在哪里犯了错误

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

推荐答案

您的地图是方形的,而大多数设备都是矩形的,所以您需要将地图保持在屏幕中心,无论您使用的是纵向还是横向模式.

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

输出是:

这篇关于如何使用 LibGdx 在 Android 上设置地图大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆