为什么舞台上的相机不需要居中 libgdx

Why camera in stage is not required to be centered in libgdx

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

如果您查看 Stage Constructor:

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);

舞台自行将摄像机位置居中。