简体   繁体   English

何时在libgdx中使用actor?有什么缺点和优点?

[英]When to use actors in libgdx? What are cons and pros?

I'm writing simple solar system simulator. 我正在写简单的太阳系模拟器。 This is my first libgdx project. 这是我的第一个libgdx项目。 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. 行星将不会被触摸,它们将仅在帧之间移动,因此动作MoveBy的第三个参数必须是帧之间的时间。 That are the cons. 这是缺点。 What are the pros for using Actors? 使用Actors的优点是什么?

The main pros for Actors are Actions, Hit testing and touch events, and Groups of Actors. Actors的主要优点是动作,命中测试和触摸事件以及演员组。

Actions make quick and easy tweening if your game logic needs that. 如果您的游戏逻辑需要,操作可以快速轻松地进行补间。

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). 你可以随时调用stage.hit(x,y)来返回第一个返回true的actor,它返回你为它编写的任何命中逻辑(通常用x,y,width,height检查边界)。 return this actor or null to keep iterating through the actors' hit methods looking for a hit actor. 返回此actor或null以继续遍历actor的命中方法以寻找命中的actor。 Null is returned if no actor is hit. 如果没有玩家被击中,则返回Null。

Hit is used for the Stage's touch events. Hit用于Stage的触摸事件。 The actor's touch methods are passed local coordinates, and the Stage handles overlapping of objects, eg 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". actor的触摸方法是传递本地坐标,舞台处理对象的重叠,例如,如果一个actor覆盖另一个actor而另一个actor不应该接收touchDown,则在覆盖actor上返回true以停止在actor上调用touchDown“下面”。 This also sets 'focus' on the actor that returns true so that Actor's touchUp will be called. 这也将“焦点”设置为返回true的actor,以便调用Actor的touchUp。

You can group actors together to perform Actions, touch events, etc on the entire Group of Actors as a single unit. 您可以将actor组合在一起,以整个作业组的形式作为一个单元执行动作,触摸事件等。

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 (eg box2d). 许多编码员使用其他逻辑来确定游戏对象状态,而不是使用scene2d动作(例如box2d)。 If you use Actors for game objects, you will probably want two Stages, one for ui and one for game world. 如果你使用Actors作为游戏对象,你可能需要两个阶段,一个用于ui,一个用于游戏世界。 If you don't use them you'll probably be using your own SpriteBatch and Camera anyway though. 如果你不使用它们,你可能会使用你自己的SpriteBatch和相机。 And keep in mind that Actors only have an abstract Draw method so you will still need to create draw logic anyway. 请记住,Actors只有一个抽象的Draw方法,所以你仍然需要创建绘制逻辑。 You'll probably keep a TextureRegion or Sprite as a private field for the Actor. 您可能会将TextureRegion或Sprite保留为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). 如果要使用自己的更新逻辑,可以覆盖act(float delta)方法以获取增量时间(如果使用Actions,则调用super.act(delta))。

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. 因此,如果您有自己的逻辑并且不会使用Stage提供的大部分内容,请节省一些资源并推出您自己的特定于应用程序的解决方案。 If you can use some of the pros without limiting needed functionality then go for a second Stage for the game logic. 如果您可以使用某些专业人员而不限制所需的功能,那么请进入游戏逻辑的第二阶段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM