运行 两个 SKActions 没有 SKAction.Sequence 的顺序(确保一个完成,然后等待 运行 另一个)

Run two SKActions in sequence without SKAction.Sequence (make sure one finishes, wait then run the other)

我正在尝试创建 2 个函数。一种沿 y 轴(向上)移动精灵(汽车),另一种将其旋转(向左或向右 90 度)。 我创建了两个函数(moveCarForward()rotateCar(angle)),它们具有几乎相同的代码,但内部的操作不同。

let waitAction = SKAction.wait(forDuration: 1)
            let rotateAction = SKAction.rotate(byAngle: angle, duration: 1)
            let doneAction = SKAction.run({ [weak self] in
                car?.removeAction(forKey: "carRotating")
            })
            let moveActionWithDone = SKAction.sequence([waitAction, rotateAction, doneAction])
            self.car!.run(moveActionWithDone, withKey: "carRotating")

现在我的问题是,在我的游戏ViewController 中,我希望能够以我想要的任何顺序调用操作。例如调用(向前,向右旋转,向前,向左旋转)但是当我 运行 代码时,操作是乱序的(或者有时如果我向前移动两次,一个总是被忽略)。我认为这是因为 运行 操作是异步执行的,所以一些操作在另一个完成之前开始 运行ning。我知道 Action Sequence 在这里很有用,但这是一个面向初学者的函数教程,我需要能够从 ViewController 中单独调用每个函数。我尝试使用 DispatchGroup(),但它要么阻塞主线程,要么让它们 运行ning 乱序(我可能用错了)。非常感谢任何帮助!

创建一组在上一个动作完成时触发的动作。

在我的示例中,我使用自定义操作来执行此操作。

var car = SKSpriteNode(imageNamed:"car")
var actionChain = [SKAction]()
var completionAction = SKAction.customAction(withDuration:0){
                           [node, elapsedTime] in
                           if self.actionChain.count == 0 || elapsedTime > 0 {return}
                           let action = self.actionChain.removeFirst()
                           node.run(SKAction.sequence([action, self.completionAction]))
                        }

func moveCarForward(){

     actionChain.append(SKAction.moveBy(x:10,y:0, duration:1))
}
func rotateCar(){

     actionChain.append(SKAction.rotate(byAngle:.pi, duration:1))
}

func driveCar(){
    car.run(completionAction)
}

func testPath(){
    moveCarForward()
    rotateCar()
    rotateCar()
    moveCarForward()
    driveCar()

}