兩個位置之間的簡單移動
為此,最好的解決方案是使用 actions。要向 Scene2D 中的演員新增新動作,請致電:
Action action = Actions.moveTo(x,y,duration);
actorObject.addAction(action);
其中 x 和 y 是目標位置,持續時間是以秒為單位的移動速度(float)。
如果你想停止這個動作(和它的演員)你可以通過呼叫:
actorObject.removeAction(action);
或者你可以通過呼叫刪除所有操作:
actorObject.clearActions();
這將立即停止動作的執行。
moveTo 動作操作 actor 的 x 和 y 屬性,因此當你將 actor 繪製到螢幕時,總是使用 getX() 和 getY()來繪製紋理。就像下面的例子一樣:
public class MovingActor extends Actor {
    private Action runningAction;
    private float speed = 2f;
    public void moveTo(Vector2 location) {
       runningAction = Actions.moveTo(location.x, location.y, speed);
       this.addAction(runningAction);
    }
    public void stopAction() {
       this.removeAction(runningAction);
    }
    public void stopAllActions() {
       this.clearActions();
    }
    @Override
    public void draw(Batch batch, float parentAlpha){
        batch.draw(someTexture, getX(), getY());
    }
}