libGDX - 恢复应用后屏幕大小调整

时间:2014-01-04 17:03:52

标签: android screen libgdx resolution resume

我在libGDX中开发2D游戏,恢复屏幕后我的分辨率出现了很大问题。

启动应用后,屏幕如下所示: initial screen http://image-upload.de/image/uRVE3f/e8022a5f11.png 请注意,图形会缩放以适合屏幕尺寸。

更改为另一个应用并恢复游戏后,屏幕如下所示: resumed screen http://image-upload.de/image/syUI3u/7ff85232d1.png 请注意,图形没有缩放。

我将LwjglApplicationConfiguration config.width和config.height设置为800和480.我使用不同的设备和分辨率测试它,效果仍然相同。我没有在resume()或resize()方法中做任何事情,我正在使用OrthographicCameracamera.setToOrtho(false, 800, 480));

我该怎么做才能使这项工作正常进行?顺便说一下,它非常适合桌面版。

编辑:相关代码部分:

AbstractBaseScreen.java

public abstract class AbstractBaseScreen {

    // constants
    protected final int screenWidth = GlobalConstants.GAME_WIDTH; //800
    protected final int screenHeight = GlobalConstants.GAME_HEIGHT; //480

    // camera
    protected OrthographicCamera camera;

    // ui
    protected AbstractBaseInputHandler inputHandler;
    protected Skin skin;
    protected BitmapFont font;
    protected Background background;
    protected Texture backgroundTexture;
    protected SpriteBatch batch;
    protected Stage stage;

    public abstract void create();
    public abstract void resize(int width, int height);
    public abstract void update();
    public abstract void render();
    public abstract void pause();
    public abstract void resume();
    public abstract void dispose();
    public void onRender() {
        update();
        render();
    }
}

屏幕示例:MainMenuScreen.java 显示两个按钮和一个背景。

public class MainMenuScreen extends AbstractBaseScreen {
    // @Override
    public void create() {
        // set spritebatch
        batch = SpriteBatchSingleton.getInstance();

        // create stage
        stage = new Stage(screenWidth, screenHeight, false);
        Gdx.input.setInputProcessor(stage);

        camera = new OrthographicCamera();
        camera.setToOrtho(false, screenWidth, screenHeight);

        // set skin
        skin = new Skin(Gdx.files.internal("data/ui.json"));

        // background
        background = new Background("backgrounds/bg_base");
        background.loadSprite();

        // buttons
        skillButton = new SkillButton(MainMenuScreen.class, skin);
        startButton = new StartButton(MainMenuScreen.class, skin);

        // add buttons to stage
        stage.addActor(skillButton.getButton());
        stage.addActor(startButton.getButton());
    }

    // @Override
    public void render() {
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

        camera.update();
        batch.setProjectionMatrix(camera.combined);

        batch.begin();
        background.draw();
        batch.end();
        stage.draw();
    }

    // @Override
    public void resize(int width, int height) {
        stage.setViewport(width, height, false);
    }

    // @Override
    public void dispose() {
        stage.dispose();
        skin.dispose();
        skillButton.dispose();
        startButton.dispose();
    }

    @Override
    public void update() {}

    @Override
    public void pause() {}

    @Override
    public void resume() {
        create();
    }
}

1 个答案:

答案 0 :(得分:1)

不要对resize()做任何事情。希望它会有所帮助:)