Java LibGDX Stage vs SpriteBatch 绘制游戏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22284429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
LibGDX Stage vs SpriteBatch to draw the Game
提问by Fabian K?nig
I have a big problem with the drawing in LibGDX. First I don′t use every physics or other complicate things in my game. I only draw sprites and put Rectangles at this for Collision Detection. I have 3 Screens in my game: splash, menu and game. So I use a Stage in the splash and menu screen but I don′t really if I use Stage in the game too or I draw with SpriteBatch? The Stage have really pros with Actions but it a little bit more complicate to use Actors. Are there other ways to do this? I don′t want to use World, because its to complicate for the beginning of game developing. What I want to use for the game?
我在 LibGDX 中绘图有一个大问题。首先,我不会在我的游戏中使用所有物理或其他复杂的东西。我只绘制精灵并在此处放置矩形以进行碰撞检测。我的游戏中有 3 个屏幕:启动画面、菜单和游戏。所以我在启动画面和菜单屏幕中使用了舞台,但如果我在游戏中也使用舞台或使用 SpriteBatch 绘图,我真的不会?Stage 在 Actions 方面确实很有优势,但使用 Actors 有点复杂。还有其他方法可以做到这一点吗?我不想使用 World,因为它对于游戏开发的开始来说很复杂。我想在游戏中使用什么?
采纳答案by Springrbua
Actually you always draw with SpriteBatch
, as Stage
uses its own SpriteBatch
. If you think you could need some of the advantages of Scene2d
s Stage
, like Action
s, Group
s or other things, you should use Stage
. In my opinion it is really easy to use. You have your own objects, which extend Image
or Actor
, override their update(delta)
methods, where you check if the Actor
should move (Keypressed or an event for the AI). Also you should detect collisions there and take care about them. Then, if you are extending Image
you don't need to care about drawing (but Image
needs Drawable
s so you will have some problems with Animation
s), if you are extending Actor
override the draw(SpriteBatch batch)
, where you call batch.draw(...)
.
Thats it.
A big disadvantage of Stage
is the sorting. The Actor
, which is drawn as first is in the background. The last Actor
overdraws all others and is in foreground. You can sort them by using the z-Index, but it is not really sure.
So you may decide to use your own kind of Stage
(it is more or less a list, containing all Actor
s, and when update
or draw
is called, it is called for every Actor
in this list), and add your own sorting there.
It depends on your game design, on your opinion and on your style. Look at some tutorials/examples and decide how you want to create your game.
其实你总是画画SpriteBatch
,就像Stage
用自己的一样SpriteBatch
。如果您认为您可能需要Scene2d
s 的一些优点Stage
,例如Action
s、Group
s 或其他东西,您应该使用Stage
. 在我看来,它真的很容易使用。您有自己的对象,它们扩展Image
或Actor
覆盖它们的update(delta)
方法,您可以在其中检查 是否Actor
应该移动(按键或 AI 的事件)。您还应该检测那里的碰撞并处理它们。然后,如果你正在扩展Image
你不需要关心绘图(但Image
需要Drawable
s 所以你会有一些Animation
s问题),如果你扩展Actor
覆盖draw(SpriteBatch batch)
,你调用的地方batch.draw(...)
. 就是这样。一个很大的缺点Stage
是排序。的Actor
,其作为第一拉伸是在背景中。最后一个Actor
透支所有其他人,并在前台。您可以使用 z-Index 对它们进行排序,但并不确定。所以你可以决定使用你自己的类型Stage
(它或多或少是一个列表,包含所有的Actor
s,当update
或被draw
调用时,它会被Actor
这个列表中的每一个调用),并在那里添加你自己的排序。这取决于您的游戏设计、您的意见和您的风格。查看一些教程/示例并决定如何创建游戏。
回答by Display Name
You can use either, it depends on what kind of game are you creating. But anyway, don't create multiple instances ofSpriteBatch
— instead create only one and pass it to Stage
constructor, because it's heavy object. (Yes, Stage
uses a SpriteBatch
under the hood)
Also, Stage
may be extremely useful if you need any on-screen controls(buttons, joystick, etc), because you can use classes from scene2d.ui
and don't reinvent the wheel (some wheels are not very easy to reinvent properly)
您可以使用任何一种,这取决于您创建的游戏类型。但无论如何,不要创建多个实例SpriteBatch
——而是只创建一个并将其传递给Stage
构造函数,因为它是重对象。(是的,在幕后Stage
使用SpriteBatch
)
此外,Stage
如果您需要任何屏幕控制(按钮、操纵杆等),这可能非常有用,因为您可以使用来自的类scene2d.ui
而不是重新发明轮子(有些轮子不是很容易正确地重新发明)