Reputation: 547
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))
}
}
Upvotes: 0
Views: 462
Reputation: 5704
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.
Upvotes: 0