如果整个“游戏世界”比视口宽数千倍,并且如果我想用scene2d来管理游戏对象为Actor
s,应该创建舞台对象与整个世界一样宽,还是应该围绕当前视口而不是整个世界的某个区域?
换句话说,具有更大宽度和高度的Stage
本身会消耗更多的内存,即使我只在小视口大小的部分渲染对象?在一个巨大的世界游戏中正确使用scene2d的舞台
8
A
回答
14
我想你错误地理解了Stage
究竟是什么。舞台本身并不具备真正的大小。您不指定宽度或高度或Stage
,您只指定视口的宽度和高度。视口就像一个窗口,只显示你的世界的一部分,也就是场景。 A Stage
是2D场景图,它会随着您的Actors
“增长”。你拥有的演员越多,你的舞台就越大(记忆方面),但它并不取决于你演员的实际演出程度。如果它们的距离非常远,并且只显示整个Stage
的很小部分,则处理效率会非常高,因为场景图细分这个巨大空间以便能够快速决定是否忽略某个Actor,或在屏幕上绘制它。
这意味着Stage
实际上正是你所需要的这种情况,你应该没有任何问题,FPS和记忆方式。 但是当然,如果您的Stage
是您的视口大小的1000倍,并且您自己知道某些演员不会很快显示,那么将其添加到舞台上还是有意义的。
0
阶段只是一个根节点,它将容纳所有参与者。它的作用是为其子女调用方法(如绘制和动作);因此只有演员的数量和复杂性才会影响内存和帧率。
对于你的情况,一个剔除方法肯定是必需的。最简单的方法是检查演员是否在视口中,是否不跳过画他。创建自定义的演员,并添加以下代码:source
public void draw (SpriteBatch batch, float parentAlpha) {
// if this actor is not within the view of the camera we don't draw it.
if (isCulled()) return;
// otherwise we draw via the super class method
super.draw(batch, parentAlpha);
}
Rectangle actorRect = new Rectangle();
Rectangle camRect = new Rectangle();
boolean visible;
private boolean isCulled() {
// we start by setting the stage coordinates to this
// actors coordinates which are relative to its parent
// Group.
float stageX = getX();
float stageY = getY();
// now we go up the hierarchy and add all the parents'
// coordinates to this actors coordinates. Note that
// this assumes that neither this actor nor any of its
// parents are rotated or scaled!
Actor parent = this.getParent();
while (parent != null) {
stageX += parent.getX();
stageY += parent.getY();
parent = parent.getParent();
}
// now we check if the rectangle of this actor in screen
// coordinates is in the rectangle spanned by the camera's
// view. This assumes that the camera has no zoom and is
// not rotated!
actorRect.set(stageX, stageY, getWidth(), getHeight());
camRect.set(camera.position.x - camera.viewportWidth/2.0f,
camera.position.y - camera.viewportHeight/2.0f,
camera.viewportWidth, camera.viewportHeight);
visible = (camRect.overlaps(actorRect));
return !visible;
}
如果您需要提高性能更进一步,你可以手动切换到决定(移动相机时前)什么是可见的,什么不是。这会更快,因为所有这些剔除计算都是在每帧都执行的,对于每个actor。所以虽然做数学而不是绘画要快很多,但大量的演员会给大量的不需要的电话。
相关问题
- 1. 游戏AI在世界中的位置
- 2. 在Flash中使用大型游戏世界
- 3. 存储大型2D游戏世界
- 4. Flash游戏世界限制
- 5. Scala - 更新游戏世界
- 6. IndexOutofbouds在游戏的世界渲染
- 7. iOS游戏世界的日历/时钟
- 8. 在Phaser中,是否有清除屏幕或清除游戏舞台/世界的方法?
- 9. 有一个简单的脚本2D游戏世界?
- 10. 压缩在iPhone游戏中使用的巨大PNG文件
- 11. 使用游戏杆或游戏手柄来遍历scene2d菜单
- 12. 绘图等距游戏世界
- 13. 代表在游戏世界中渲染的光
- 14. 跳舞机游戏
- 15. 点击3D世界统一的3D游戏对象
- 16. PETSc和MPI COMM世界正确使用
- 17. 游戏开发 - 使用屏幕坐标与世界坐标
- 18. 使用planet.osm以.pbf格式生成游戏世界
- 19. 如何动态地构建地图的大型世界在CraftyJS游戏
- 20. 如何在mysql中存储2D游戏世界
- 21. 世界空间帆布没有在游戏中显示
- 22. 在RPG游戏中存储世界信息
- 23. 如何处理在iPhone上使用cocos2d进行换行的游戏世界
- 24. 如何使用图像文件在2D游戏中加载世界/关卡?
- 25. 在游戏世界中存储和检索对卸载对象的引用
- 26. Libgdx scene2d 3D游戏与2D逻辑?
- 27. 在XCode 4.5上为一个巨大的游戏项目提高构建时间
- 28. 在SpriteKit游戏中使用didBeginContact的正确方法 - Swift
- 29. 没有滚动世界的Java 2D游戏摄像头
- 30. Python文本冒险 - 与游戏世界互动的物品