为什么舞台上的摄像机不需要在libgdx中居中

时间:2019-01-15 05:41:01

标签: libgdx game-physics physics game-development

默认情况下,如果摄像机的(0,0)位于舞台的(0,0),那么舞台摄像机如何能够看到完整的舞台视图。如果不调用视口的更新方法,也不调用相机位置设置方法。

1 个答案:

答案 0 :(得分:3)

如果您关注Stage构造函数:

public Stage (Viewport viewport, Batch batch) {
    if (viewport == null) throw new IllegalArgumentException("viewport cannot be null.");
    if (batch == null) throw new IllegalArgumentException("batch cannot be null.");
    this.viewport = viewport;
    this.batch = batch;

    root = new Group();
    root.setStage(this);

    viewport.update(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true);
}

我们在最后一行看到viewport.update(),其中width,height和true为参数。 让我们看一下这个viewport.update()方法:

public void update (int screenWidth, int screenHeight, boolean centerCamera) {
    apply(centerCamera);
}

现在让我们看一下apply()方法。我们知道centerCamera是真的:

public void apply (boolean centerCamera) {
    HdpiUtils.glViewport(screenX, screenY, screenWidth, screenHeight);
    camera.viewportWidth = worldWidth;
    camera.viewportHeight = worldHeight;
    if (centerCamera) camera.position.set(worldWidth / 2, worldHeight / 2, 0);
    camera.update();
}

在这里我们找到答案:if (centerCamera) camera.position.set(worldWidth / 2, worldHeight / 2, 0);

舞台将摄像头的位置居中放置。