LibGdx-某些设备的 ViewPort 宽度和高度不匹配
LibGdx-ViewPort width and height does not match for some devices
我正在为我的 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 的设备中,只有游戏的中间部分 visible.I 尝试为更高分辨率的设备使用拉伸视口,但它会拉伸所有游戏元素。
我怎样才能让我的游戏在所有设备上正常运行?我需要更改视口吗?
我会推荐 ExtendViewprot 使用,因为它通过在一个方向上扩展世界来保持世界纵横比没有黑条。使用世界宽度和高度而不是屏幕宽度和高度的所有位置,并根据您的设备宽度和高度在调整大小方法中调整视口大小。
您的资源大小应符合世界宽度和高度。
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();
我为所有更高分辨率的设备使用 1280x960 的视口宽度,同时为屏幕宽度小于 1300 的所有其他设备保持 1280x800 分辨率。
在绘制背景时,我使用视口宽度和高度来设置这样的大小。
bgSprite=new Sprite(bgTexture);
bgSprite.setPosition(Constants.BG_X,Constants.BG_Y);
bgSprite.setSize(game.viewPort.getWorldWidth(),game.viewPort.getWorldHeight());
不确定这样做是否正确,但这样做解决了我的问题。
我正在为我的 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 的设备中,只有游戏的中间部分 visible.I 尝试为更高分辨率的设备使用拉伸视口,但它会拉伸所有游戏元素。
我怎样才能让我的游戏在所有设备上正常运行?我需要更改视口吗?
我会推荐 ExtendViewprot 使用,因为它通过在一个方向上扩展世界来保持世界纵横比没有黑条。使用世界宽度和高度而不是屏幕宽度和高度的所有位置,并根据您的设备宽度和高度在调整大小方法中调整视口大小。
您的资源大小应符合世界宽度和高度。
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();
我为所有更高分辨率的设备使用 1280x960 的视口宽度,同时为屏幕宽度小于 1300 的所有其他设备保持 1280x800 分辨率。 在绘制背景时,我使用视口宽度和高度来设置这样的大小。
bgSprite=new Sprite(bgTexture);
bgSprite.setPosition(Constants.BG_X,Constants.BG_Y);
bgSprite.setSize(game.viewPort.getWorldWidth(),game.viewPort.getWorldHeight());
不确定这样做是否正确,但这样做解决了我的问题。