我如何支持(制定算法)libgdx以支持多种屏幕分辨率?我使用带有如下参数的 if 语句使我的应用程序在 HTC Tattoo 上运行:

if (Gdx.input.getX()==40) {

使其在更大屏幕上运行的好算法是什么?我尝试了这个但没有任何结果:

publis static int translatex() {
     float p = (float)Gdx.graphics.getHeight()*340;
     return (int) p*Gdx.input.getX();
}

340 是我在 HTC Tattoo 上使用的基准 x(手机上的 x 分辨率)。那么......我怎样才能制作一个支持绝对值大屏幕的函数。我不想改变 if 语句。

有帮助吗?

解决方案

这很容易。您基本上为您的本地分辨率构建了整个程序,但在处理定位和纹理大小的一切中都有这样的东西:

private void resize()
{
    float x = Gdx.graphics.getWidth();
    float y = Gdx.graphics.getHeight();

    float changeX = x / assumeX; //being your screen size that you're developing with
    float changeY = y / assumeY;

    position = new Vector2(position.x * changeX, position.y * changeY);
    width = startWidth * changeX;
    height = startHeight * changeY;
    bounds = new Vector2 (position.x, (Gdx.graphics.getHeight() - position.y) - height);

}
.

基本上您正在做的是拍摄生成的每个对象并运行它的东西,这取决于分辨率x / y值的变化。进一步来自本地,它将是更大的。当检查某些东西时在某个地方或将某些东西放在某处时,始终将其代码为您的所需分辨率,但在显示它之前通过调整大小函数运行或让它与您的其余部分交互。

其他提示

请参阅更优雅的解决方案 http://www.java-gaming.org/index.php?topic=25685.0 (另请参阅内特对该解决方案的评论)。

另外,如果您使用 scene2d, ,请注意当前(libgdx 0.9.6) stage.setViewport 方法应该具有此功能,但它的行为并不像人们预期的那样。

更新: setViewPort 已修复,因此这可以按预期工作。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top