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


基础教程推荐
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01