反转 SKAction 序列中的每个单独动作而不反转所有动作的顺序

Reversing each individual action in an SKAction sequence without reversing the order of all actions

我正在尝试创建一个 SKAction 序列,然后单独反转每个动作,而不反转整个顺序。

我最初是通过 SKAction 对象中的 reverse() 方法尝试的,但这并不是我想要的。 From the documentation:

This action is reversible; it creates a new sequence action that reverses the order of the actions. Each action in the reversed sequence is itself reversed. For example, if an action sequence is {1,2,3}, the reversed sequence would be {3R,2R,1R}.

我需要 {1R,2R,3R} 作为新操作。例如,如果我有:

let rotate = SKAction.rotate(byAngle: -CGFloat.pi / 2, duration: 0.5)
let scale = SKAction.scale(by: 2, duration: 0.5)
let action = SKAction.sequence([rotate, scale])

我想对 action 动作应用一些函数来产生一个新的动作,这相当于:

let rotate = SKAction.rotate(byAngle: CGFloat.pi / 2, duration: 0.5)
let scale = SKAction.scale(by: 0.5, duration: 0.5)
let action = SKAction.sequence([rotate, scale])

有没有一种方法可以在不单独编写翻转动作的情况下实现这一点?为此,我认为有可能将序列分解为单独的动作,将它们反转,并从中构建一个新动作,但我似乎无法找到一种方法来打破 rotation 回来再次进入两个单独的旋转。

您可以通过创建另一个序列来完成此操作,但操作相反,因此:

let actionReversed = SKAction.sequence([rotate.reversed(), scale.reversed()])

如果你想运行动作“前进”,完成后“反转”它们,你可以使用这样的东西:

  yourspritenode!.run(action) { [self] in
    yourspritenode!.run(actionReversed)
  }