gpt4 book ai didi

java - Libgdx ExtendViewport 无法与 Scene2D UI 一起正常工作

转载 作者:太空宇宙 更新时间:2023-11-04 09:12:15 25 4
gpt4 key购买 nike

我需要在游戏中使用 Extendview 端口,它在没有 scene2d 的情况下工作正常,但是当我添加 scene2d 时,我在顶部看到一个大条,而不是顶部和底部的 2 个条。

完整代码如下:

public class MenuScreen extends ScreenAdapter {
private SpinBlaster game;
private OrthographicCamera camera;
private Viewport viewport;
private Stage stage;
private Sprite backgroundSprite = new Sprite();

public MenuScreen(SpinBlaster game) {
this.game = game;
}

public void show() {
camera = new OrthographicCamera();
camera.position.set(Constants.CENTER_X, Constants.CENTER_Y, 0);
viewport = new ExtendViewport(Constants.VIRTUAL_W, Constants.VIRTUAL_H, camera);

stage = new Stage(viewport);
Gdx.input.setInputProcessor(stage);
}

public void render(float delta) {
Gdx.gl.glClearColor(0,1,0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

game.batch.setProjectionMatrix(camera.combined);
game.batch.begin();
backgroundSprite.setRegion(AssetLoader.atlas.findRegion("background"));
backgroundSprite.setPosition(0,0);
backgroundSprite.setSize(Constants.VIRTUAL_W, Constants.VIRTUAL_H);
backgroundSprite.setColor(Constants.DARK_GRAY);
backgroundSprite.draw(game.batch);
game.batch.end();

stage.draw();
stage.act(delta);
}

public void resize(int width, int height) {
viewport.update(width, height);
camera.update();
}

enter image description here .

最佳答案

通过将相机位置设置在viewport.update下方修复了该问题

public void resize(int width, int height) {
viewport.update(width, height);
camera.position.set(Constants.CENTER_X, Constants.CENTER_Y, 0); // fixed
camera.update();
}

关于java - Libgdx ExtendViewport 无法与 Scene2D UI 一起正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59542169/

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