简体   繁体   English

精灵在不同分辨率下的位置

[英]Position of sprites at different resolutions

I know it is an old subject but here is my way to dealing with different resolutions 我知道这是一个古老的话题,但这是我处理不同分辨率的方法

I get sprites x and y then divide them by 800 and 480 我得到精灵x和y,然后将它们分别除以800和480

For example if I had an sprite at 150 and 50 on 800 and 480 it will be at 0.1875 * Gdx.graphics.getWidth() and 0.1041 * Gdx.graphics.getHeight() on another screen resolutions 例如,如果我在800和480上的sprite分别为150和50,则在另一个屏幕分辨率下,它将为0.1875 * Gdx.graphics.getWidth()和0.1041 * Gdx.graphics.getHeight()

but the problem is this doesn't go well for all resolutions as there are big variations when sprite is at high resolution in comparison to small one 但问题是,这并非在所有分辨率下都适用,因为与小分辨率相比,当精灵处于高分辨率时会有很大差异

Is there another way to do this ? 还有另一种方法吗?

Use Viewports : https://github.com/libgdx/libgdx/wiki/Viewports 使用Viewportshttps : //github.com/libgdx/libgdx/wiki/Viewports

The Viewport does basically all the resolution math for you. Viewport基本上可以为您完成所有分辨率数学运算。

This is how it works: You choose a world size of your choice. 它是这样工作的:您选择自己选择的世界大小。 For example: 400 width, 300 height. 例如:400宽度,300高度。 The unit does not matter. 单位无关紧要。

When you place a sprite at position (200, 150) the left bottom corner of that sprite will be in the center of the screen no matter which resolution. 当您将精灵放置在位置(200、150)时,无论哪种分辨率,该精灵的左下角都将位于屏幕的中心。

The calculation from your world coordinate to the screen coordinate will be done for you by the Viewport . Viewport将为您完成从世界坐标到屏幕坐标的计算。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM