Search code examples
iosswiftsprite-kitskaction

How to change variable within SKAction sequence?


When my viewDidLoad gets called, I run the following action:

    runAction(SKAction.repeatActionForever(
        SKAction.sequence([
            SKAction.runBlock(chainsawDropper),
            SKAction.waitForDuration(waitDuration)
            ])
        ))

Then in my update method I decrease the "waitDuration" by 1. The problem is that the sequence continues to run but never uses the updated "waitDuration". How can I achieve this?


Solution

  • if you run the action with a tag then you can override it at any time:

    func updateActionForDuration(duration:NSTimeInterval) {
        self.runAction(  SKAction.repeatActionForever(SKAction.sequence(
             [SKAction.runBlock(chainsawDropper),
             SKAction.waitForDuration(duration)])), 
        withKey: "action key")
    }