何时在 libgdx 中使用演员?什么是缺点和优点?
我正在编写简单的太阳系模拟器.这是我的第一个 libgdx 项目.我在主菜单中使用了舞台和演员,并且非常方便,尤其是触摸事件处理.但是......看看这些例子,我发现没有人在实际游戏逻辑中使用演员.如果我应该使用actor作为行星类的父级,或者只是编写我自己的类,我会徘徊.行星是不可触摸的,它们只会在帧之间移动,因此动作 MoveBy 的第三个参数必须是帧之间的时间.这就是缺点.使用 Actors 的优点是什么?
I'm writing simple solar system simulator. This is my first libgdx project. I'm using a Stage and Actors for the main menu and is pretty handy especially touch events handling. But ... looking at the examples i see nobody uses actors in actual game logic. I wander if i should use actor as a parent of planet class or just write my own class tor that. The planets won't be touchable and they will be moved only between the frames so the third parameter of action MoveBy will have to be time between frames. That are the cons. What are the pros for using Actors?
推荐答案
Actor 的主要优点是动作、命中测试和触摸事件以及 Actor 组.
The main pros for Actors are Actions, Hit testing and touch events, and Groups of Actors.
如果您的游戏逻辑需要,动作可以快速轻松地进行补间.
Actions make quick and easy tweening if your game logic needs that.
您可以随时调用 stage.hit(x, y) 以返回第一个返回 true 的参与者,无论您为它编写的任何命中逻辑(通常检查 x、y、宽度、高度的边界).返回此演员或 null 以继续遍历演员的命中方法以寻找命中演员.如果没有命中 Actor,则返回 Null.
You can call stage.hit(x, y) at any time to return the first actor that returns true to whatever hit logic you wrote for it (usually checking bounds with x, y, width, height). return this actor or null to keep iterating through the actors' hit methods looking for a hit actor. Null is returned if no actor is hit.
Hit 用于舞台的触摸事件.演员的触摸方法传递局部坐标,舞台处理对象的重叠,例如如果一个actor覆盖了另一个actor,使得另一个actor不应该接收touchDown,则在覆盖actor上返回true以停止对下面"actor的touchDown调用.这也将焦点"设置在返回 true 的 Actor 上,以便调用 Actor 的 touchUp.
Hit is used for the Stage's touch events. The actor's touch methods are passed local coordinates, and the Stage handles overlapping of objects, e.g. if an actor covers another actor such that the other actor shouldn't receive touchDown, return true on the covering actor to stop the calling of touchDown on actors "beneath". This also sets 'focus' on the actor that returns true so that Actor's touchUp will be called.
您可以将 Actor 组合在一起,以将整个 Actor 组作为一个单元执行动作、触摸事件等.
You can group actors together to perform Actions, touch events, etc on the entire Group of Actors as a single unit.
一些缺点:演员需要一个在某种程度上限制功能的舞台.许多编码人员使用其他逻辑来确定游戏对象状态,而不是 scene2d 动作(例如 box2d).如果您将 Actors 用于游戏对象,您可能需要两个阶段,一个用于 ui,一个用于游戏世界.如果你不使用它们,你可能会使用你自己的 SpriteBatch 和 Camera.请记住,Actor 只有一个抽象的 Draw 方法,所以无论如何您仍然需要创建绘制逻辑.您可能会将 TextureRegion 或 Sprite 作为 Actor 的私有字段.如果您想使用自己的更新逻辑,您可以重写 act(float delta) 方法来获取增量时间(如果使用 Actions,请调用 super.act(delta)).
Some Cons: Actors require a stage which limits functionality somewhat. Many coders use other logic to determine game object state, rather than the scene2d Actions (e.g. box2d). If you use Actors for game objects, you will probably want two Stages, one for ui and one for game world. If you don't use them you'll probably be using your own SpriteBatch and Camera anyway though. And keep in mind that Actors only have an abstract Draw method so you will still need to create draw logic anyway. You'll probably keep a TextureRegion or Sprite as a private field for the Actor. If you want to use your own update logic, you can override the act(float delta) method to get the delta time (call super.act(delta) if you use Actions).
因此,如果您有自己的逻辑并且不会使用 Stage 提供的大部分功能,请节省一些资源并推出您自己的特定于应用程序的解决方案.如果您可以在不限制所需功能的情况下使用某些专业人士,那么请为游戏逻辑进行第二阶段.
So if you have your own logic and won't use much of what Stage has to offer, save some resources and roll your own application-specific solution. If you can use some of the pros without limiting needed functionality then go for a second Stage for the game logic.
相关文章