[英]Remove Actors from Stage?
我使用LibGDX並在我的游戲中只移動相機。 昨天我創造了一種在游戲中占據一席之地的方法。 我正在嘗試克隆Flappy Bird,但我在繪制正在屏幕上移動的地面時遇到了問題。 在每次渲染調用中,我都會向Stage
添加一個新的Actor
,但是幾次之后繪圖就不再流動了。 每秒幀數下降得非常快。 還有另一種方法可以在游戲中取得進展嗎?
如果我正確閱讀,你的問題是,一旦演員離開屏幕,它們仍在處理並導致滯后,你希望它們被刪除。 如果是這種情況,您可以簡單地遍歷舞台中的所有演員,將他們的坐標投影到窗口坐標,並使用它們來確定演員是否在屏幕外。
for(Actor actor : stage.getActors())
{
Vector3 windowCoordinates = new Vector3(actor.getX(), actor.getY(), 0);
camera.project(windowCoordinates);
if(windowCoordinates.x + actor.getWidth() < 0)
actor.remove();
}
如果演員x在窗口中的坐標加上它的寬度小於0,則演員已完全滾動離開屏幕,並且可以被移除。
從@kabb稍微調整一下解決方案:
for(Actor actor : stage.getActors()) {
//actor.remove();
actor.addAction(Actions.removeActor());
}
從我的經驗,呼吁actor.remove()
而迭代stage.getActors()
將打破循環 ,因為它是從正在積極地迭代數組移除演員。
一些類似數組的類會在這種情況下拋出
ConcurrentModificationException
作為警告。
所以...解決方法是告訴演員稍后用Action
刪除自己
actor.addAction(Actions.removeActor());
或者......如果您因為某些原因等不及要刪除actor,可以使用SnapshotArray
:
SnapshotArray<Actor> actors = new SnapshotArray<Actor>(stage.getActors());
for(Actor actor : actors) {
actor.remove();
}
從其父級中刪除actor的最簡單方法是調用其remove()
方法。 例如:
//Create an actor and add it to the Stage:
Actor myActor = new Actor();
stage.addActor(myActor);
//Then remove the actor:
myActor.remove();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.