問題描述
我正在編寫簡單的太陽系模擬器.這是我的第一個 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.
這篇關于何時在 libgdx 中使用演員?什么是缺點和優點?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!