我需要在我的游戏中使用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();
}
。
最佳答案
通过将相机位置设置在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/