libgdx - 为什么舞台上的相机不需要在libgdx中居中
问题描述
如果相机的(0,0)默认位于舞台的(0,0),舞台相机如何能够看到完整的舞台视图。如果没有调用视口的更新方法,也没有调用相机位置设置方法。
解决方案
如果您查看舞台构造函数:
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() 中看到了宽度、高度和 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);
舞台以她自己的方式将摄像机位置居中。
推荐阅读
- xslt-2.0 - XSLT 2.0 需要为每位员工输出最多 10 条记录
- ruby - 如何在 Rails 6 中更新 @model.submodel
- javascript - 从参数中的对象中删除属性与从对象中删除属性
- c# - 在 xamarin.forms (ios) 中启动演示应用程序时出错
- excel - 是否可以编写一个 MS Excel 将打开并显示带有千位分隔符的数字的文本文件?
- ios - ios AppDelegate.swift 上的 simple_auth_flutter 导入更改无法启动
- javascript - IntersectionObserver 无法向上滚动
- javascript - Javascript 从字符串中提取锚标记并从中创建新的 React 组件
- javascript - 在 React Native 中使用 position: 'Absolute' 和 display: 'None' 仍然会渲染组件
- azure-devops - 我在 Azure DevOps 中安装了交付计划(作为 Marketplace 的扩展),但在 Boards 菜单中看不到