方向更改后正确调整libgdx大小

时间:2015-08-19 15:52:50

标签: android libgdx orientation

我在舞台上有一些方形图像。出于某种原因,它们在调整大小后变成矩形。如何正确调整大小?以及如何手动调整游戏大小?

景观 landscape

肖像 portrait

如你所见,它们有230x230,所以它们不能是矩形

   imagesEndPos[i] = new Image(skin1, "square");
                imagesEndPos[i].setWidth(230);
                imagesEndPos[i].setHeight(230);
                imagesEndPos[i].setPosition(pointIEnd.getX(), pointIEnd.getY());
                stage.addActor(imagesEndPos[i]);

create()方法

   public void create() {
        stage = new Stage();
        Gdx.input.setInputProcessor(stage);

        soundContainter=new SoundContainter();
        soundContainter.init(taskSheet,audios);

        camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        camera.setToOrtho(true, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        camera.update();
}

resize()方法

public void resize(int width, int height) {
    stage.getViewport().setScreenSize(width, height);
    camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    camera.setToOrtho(true, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

    if(width<height) {
        float w=camera.viewportWidth;
        float h=camera.viewportHeight;
        camera.viewportWidth = h;
        camera.viewportHeight = w;

    }else{
        camera.viewportWidth = width;
        camera.viewportHeight = height;
    }

    camera.update();
}

渲染()

public void render() {
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    stage.getBatch().setProjectionMatrix(camera.combined);
    stage.act(Gdx.graphics.getDeltaTime());
    stage.draw();
}

1 个答案:

答案 0 :(得分:2)

因为你正在填写图片。当它改变方向时,还要确保用camera.viewportHeight

切换camera.viewportWidth