LibGdx-ViewPort width and height does not match for some devices(某些设备的 LibGdx-ViewPort 宽度和高度不匹配)
问题描述
我正在为我的 libGdx 风景游戏使用这样的视口.
I am using a Viewport for my libGdx landscape game like this.
public static final int WORLD_WIDTH = 1280;
public static final int WORLD_HEIGHT = 800;
camera = new OrthographicCamera();
float aspectRatio = Constants.WORLD_WIDTH / Constants.WORLD_HEIGHT;
ViewPort viewPort = new FillViewport(WORLD_WIDTH * aspectRatio,WORLD_HEIGHT, camera);
我正在使用这个宽度和高度方面的所有位置.
I am using all positions in terms of this width and height.
除了屏幕分辨率大于 1300 的设备外,它在所有设备上都可以正常工作.在分辨率大于 1300 的设备中,只能看到游戏的中间部分.我尝试在分辨率更高的设备上使用拉伸视口,但它会拉伸所有游戏元素.
It is working fine in all devices except device that have screen resolution greater than 1300. In device that have greater resolution than 1300,only middle part of the game is visible.I tried using stretched viewport for the greater resolution device,but it is giving all game elements stretched.
如何让我的游戏在所有设备上都能正常运行?是否需要更改视口?
How can I make my game working in all devices fine?Do I need to change the viewport?
推荐答案
我终于成功了.我不确定这是否是解决此类问题的正确方法.我是这样做的;
Finally I made it work. I am not sure whether this is the correct way to solve such problems. I did it like this;
camera = new OrthographicCamera();
if(screenHeight>1300)
{
Constants.WORLD_WIDTH=1280;
Constants.WORLD_HEIGHT=960;
}
else{
Constants.WORLD_WIDTH=1280;
Constants.WORLD_HEIGHT=800;
}
viewPort = new FillViewport(Constants.WORLD_WIDTH, Constants.WORLD_HEIGHT, camera);
viewPort.apply();
我为所有分辨率更高的设备使用了 1280x960 的 viewPort 宽度,同时为所有其他屏幕宽度小于 1300 的设备保持 1280x800 分辨率.在绘制背景时,我使用视口宽度和高度来设置大小.
I used a viewPort width of 1280x960 for all greater resolution devices while keeping 1280x800 resolution for all other devices that has screen width less than 1300. While drawing background I used viewport width and height to set the size like this.
bgSprite=new Sprite(bgTexture);
bgSprite.setPosition(Constants.BG_X,Constants.BG_Y);
bgSprite.setSize(game.viewPort.getWorldWidth(),game.viewPort.getWorldHeight());
不确定这是正确的方法,但这样做解决了我的问题.
Not sure this the right way,but doing this solved my problem.
这篇关于某些设备的 LibGdx-ViewPort 宽度和高度不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:某些设备的 LibGdx-ViewPort 宽度和高度不匹配


基础教程推荐
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01