0
votes

I'm coding a game where I have a character following another character and I'd like to make it so that the second character jumps 1 or 2 seconds after the first character jumps. How might I accomplish this?

Here is my method that applies the impulse:

override func touchesBegan(touches: Set, withEvent event: UIEvent) { /* Called when a touch begins */

    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))


            }
}
1
Are you using GameplayKit for this?Nikita Leonov
I'm not using anything except what's included in SpriteKit.kriskendall99

1 Answers

0
votes

As you already have follow action, you need just to chain it with delay action. There is a class method waitForDuration that allows you to initialize such Action fast.

Here is a code that will do delay and apply impulse afterward:

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)
}

Code uses SKAction additions for applyImpulse available starting from iOS 9.