gpt4 book ai didi

java - Fitviewport 无法正常工作

转载 作者:太空宇宙 更新时间:2023-11-04 06:20:46 26 4
gpt4 key购买 nike

当我使用正确的宽高比启动游戏时,当我调整窗口大小时,我的游戏可以很好地缩放,但当我使用不同的宽高比启动应用程序时,就会出现问题。

我在主游戏类中设置了这些全局变量:

public static final int VirtualWidth = 720;
public static final int VirtualHeight = 1280;

在屏幕类中,我使用 fitviewport。

private Stage gameStage;
private OrthographicCamera camera;
private SpriteBatch batch;

private Viewport vp = new FitViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

public GameScreen()
{
batch = new SpriteBatch();
camera = new OrthographicCamera(ShufflePuzzle.VirtualWidth, ShufflePuzzle.VirtualHeight);
camera.setToOrtho(false, ShufflePuzzle.VirtualWidth, ShufflePuzzle.VirtualHeight);

FitViewport vp = new FitViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), camera);
vp.update(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

gameStage = new Stage(vp, batch);

Gdx.input.setInputProcessor(gameStage);
}

@Override
public void render(float delta) {
Gdx.gl.glClearColor(.2f, .2f, .3f, 1);
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);

gameStage.act();
gameStage.draw();

}

@Override
public void resize(int width, int height) {
vp.update(width,height);
gameStage.getViewport().update(width,height, false);
}

据我所知,我必须将相机设置为屏幕的实际尺寸,并将视口(viewport)设置为我的虚拟值。我确实尝试过改变这一点,得到了完全相同的结果。这是我的桌面配置:

    //The size i made my game for, here everything works like a charm.
// If i resize my viewport manually to 1024x768 the screen gets fitted like it should.
config.width=720 / 2;
config.height=1280 / 2;

//When I use below config my stage will not fit the screen.
config.width = 1024 * 2;
config.height = 768 * 2;

最佳答案

实例化视口(viewport)时,您需要使用虚拟尺寸,而不是相机。无论如何,相机值都将被视口(viewport)忽略并覆盖。

所以这样做:

camera = new OrthographicCamera();
FitViewport vp =
new FitViewport(ShufflePuzzle.VirtualWidth, ShufflePuzzle.VirtualHeight, camera);

此外,构造函数的 vp 引用隐藏了成员 vp,因此不清楚哪个是哪个。我想不出为什么您需要两个具有相同参数的单独视口(viewport)的原因。但如果这样做,作为成员变量的变量也应该使用虚拟大小进行实例化,而不是窗口的实际大小(如果您在此类的构造函数之外实例化它,则在实例化时无论如何它都可能为零)。

关于java - Fitviewport 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27445304/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com