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

我正在使用Viewport作为我的libGdx风景游戏。

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); 

我正在使用这个宽度和高度的所有位置。

它在除屏幕分辨率大于1300的设备之外的所有设备中都能正常工作。在分辨率高于1300的设备中,只有游戏的中间部分可见。我尝试使用拉伸视口来获得更高分辨率的设备,但它给出的是所有游戏元素都拉长了

如何让我的游戏在所有设备上运行良好?我需要更改视口吗?

我建议使用ExtendViewprot ,因为它通过向一个方向扩展世界来保持世界宽高比而没有黑条。 根据世界宽度和高度使用所有位置而不是屏幕宽度和高度,并根据设备宽度和高度调整resize方法中的视口大小。

您的资源大小应该与世界宽度和高度相关。

 public class MyGdxGame extends ApplicationAdapter { Stage stage; public static final int WORLD_WIDTH = 800; public static final int WORLD_HEIGHT = 480; @Override public void create() { ExtendViewport extendViewport=new ExtendViewport(WORLD_WIDTH,WORLD_HEIGHT); stage=new Stage(extendViewport); //logical pixels of your current device, device specific //float w=Gdx.graphics.getWidth(); //screen width //float h=Gdx.graphics.getHeight();// screen height Image image=new Image(new Texture("badlogic.jpg")); image.setPosition(WORLD_WIDTH/2,WORLD_HEIGHT/2, Align.center); //consider world width and height instead of screen width and height stage.addActor(image); stage.setDebugAll(true); } @Override public void render() { Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); Gdx.gl.glClearColor(0,0,0,1); stage.act(); stage.draw(); } @Override public void resize(int width, int height) { stage.getViewport().update(width,height); stage.getCamera().position.set(WORLD_WIDTH/2,WORLD_HEIGHT/2,0); stage.getCamera().update(); } @Override public void dispose() { stage.dispose(); } } 

最后我做到了。 我不确定这是否是解决此类问题的正确方法。 我是这样做的;

  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(); 

我为所有更高分辨率的设备使用了viewPort宽度1280×960,同时为屏幕宽度小于1300的所有其他设备保持1280×800分辨率。在绘制背景时,我使用视口宽度和高度来设置这样的大小。

 bgSprite=new Sprite(bgTexture); bgSprite.setPosition(Constants.BG_X,Constants.BG_Y); bgSprite.setSize(game.viewPort.getWorldWidth(),game.viewPort.getWorldHeight()); 

不确定这是正确的方法,但这样做解决了我的问题。