某些设备的 LibGdx-ViewPort 宽度和高度不匹配

2022-01-12 00:00:00 android java 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.

相关文章