嗨我在libgdx中切换屏幕时遇到问题。我正在建造一个小行星游戏克隆。因此,首先我的MainMenuScreen类(使用Fitviewport)被渲染,然后我将setScreen()调用到GameScreen(GameScreen不使用Fitviewport),除了第二个屏幕呈现就像使用Fitviewport一样。如果我调整第二个屏幕的大小,则整个窗口用于渲染。为什么会这样?这是一些图片......
我希望GameScreen使用所有窗口区域进行渲染,而不是像MainMenu那样有黑条。我也没有在GameScreen中使用任何Fitviewport。
以下是我的MainMenuScreen类的相关部分:
public class MainMenuScreen implements Screen
{
private static final String TAG = "MainMenu";
private static final int VIRTUAL_WIDTH = 400;
private static final int VIRTUAL_HEIGHT = 400;
MyGdxGame game;
...
public MainMenuScreen(MyGdxGame game)
{
this.game = game;
viewport = new FitViewport(VIRTUAL_WIDTH, VIRTUAL_HEIGHT);
stage = new Stage(viewport);
// Play button listener
btnPlay.addListener( new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
Gdx.app.log(TAG, "PLAY");
MainMenuScreen.this.game.setScreen(MainMenuScreen.this.game.gameScreen);
};
});
....
}
这是我的GameScreen类:
MyGdxGame game;
OrthographicCamera guiCam;
World world;
WorldRenderer renderer;
public GameScreen(MyGdxGame game, SpriteBatch batch)
{
this.game = game;
guiCam = new OrthographicCamera(400, 400);
guiCam.position.set(400 / 2, 400 / 2, 0);
world = new World();
renderer = new WorldRenderer(game, batch, world);
}
public void draw()
{
GL20 gl = Gdx.gl;
gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
renderer.render();
}
@Override
public void show()
{
world.createLevel();
}
@Override
public void render(float delta)
{
draw();
}
@Override
public void resize(int width, int height)
{
}
@Override
public void pause()
{
}
@Override
public void resume()
{
}
@Override
public void hide()
{
}
@Override
public void dispose()
{
}
答案 0 :(得分:4)
好的家伙我发现了问题,这行代码:
public void resize(int width, int height)
{
viewport.update(width, height, true);
}
每当更新视口时,您还要更新底层的opengl视口,这会导致黑条持续到第二个屏幕(https://github.com/libgdx/libgdx/wiki/Viewports)。因此,如果要将其重置为正常,则必须在第二个屏幕show()方法(https://github.com/libgdx/libgdx/wiki/Scene2d)中使用以下代码行:
Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());