如何使用 LibGdx 在 Android 上设置地图大小

How to set map size on Android using LibGdx(如何使用 LibGdx 在 Android 上设置地图大小)
本文介绍了如何使用 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 上设置地图大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)