Libgdx ExtendViewport无法在Scene2D UI中正常使用

问题描述 投票:1回答:1

我需要在我的游戏中使用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

java libgdx scene2d
1个回答
0
投票

通过将相机位置设置在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();
}
© www.soinside.com 2019 - 2024. All rights reserved.