LibGdx-ViewPort的宽度和高度与某些设备不匹配 [英] LibGdx-ViewPort width and height does not match for some devices

查看:99
本文介绍了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的宽度和高度与某些设备不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆