在 SpriteKit 中延迟 "Apply Impulse"

Delay "Apply Impulse" in SpriteKit

我正在编写一个游戏,其中我有一个角色跟随另一个角色,我想让第二个角色在第一个角色跳跃后 1 或 2 秒跳跃。我怎样才能做到这一点?

这是我施加脉冲的方法:

override func touchesBegan(touches: Set, withEvent event: UIEvent) { /* 触摸开始时调用 */

    for touch in (touches as! Set<UITouch>) {
        let location = touch.locationInNode(self)
        fish.physicsBody?.velocity = (CGVectorMake(0,0))
        fisherman.physicsBody?.velocity = (CGVectorMake(0,0))
        fish.physicsBody?.applyImpulse(CGVectorMake(0, 1000))
        fisherman.physicsBody?.applyImpulse(CGVectorMake(0, 3000))


            }
}

因为你已经有了跟随动作,你只需要将它与延迟动作链接起来。有一个 class 方法 waitForDuration 可以让你快速初始化这样的 Action。

这里是一个代码,它会延迟并在之后应用脉冲:

for touch in (touches as! Set<UITouch>) {
        fish.physicsBody?.velocity = (CGVectorMake(0,0))
        fisherman.physicsBody?.velocity = (CGVectorMake(0,0))

        let delay = SKAction.waitForDuration(2.0)

        let fishApplyImpulse = SKAction.applyImpulse(CGVectorMake(0, 1000),
               duration sec: 0.0)
        let fishActions = SKAction.sequence([delay, fishApplyImpulse])

        let fishermanApplyImpulse = SKAction.applyImpulse(CGVectorMake(0, 3000),
               duration sec: 0.0)


        let fishermanActions = SKAction.sequence([delay, fishermanApplyImpulse])


        fish.runAction(fishActions)
        fisherman.runAction(fishermanActions)
}

代码使用 SKAction 添加 applyImpulse 从 iOS 9.

开始可用