You can use SKAction sequence and it's waitForDuration:withRange: method, like this:
//By default, the sprite is initialized with downTexture
let sprite = SKSpriteNode(texture: downTexture)
sprite.position = CGPoint(x: 200, y: 200)
addChild(sprite)
let sequence = SKAction.sequence([
SKAction.runBlock({NSLog("Up texture set")}), //Added just for debugging to print a current time
SKAction.setTexture(upTexture),
SKAction.waitForDuration(2.5, withRange: 3.0), //Wait between 1.0 and 4.0 seconds
SKAction.runBlock({NSLog("Down texture set")}),
SKAction.setTexture(downTexture),
SKAction.waitForDuration(0.65, withRange: 0.7),//Wait between 0.3 and 1.0 seconds
])
let action = SKAction.repeatActionForever(sequence)
sprite.runAction(action, withKey: "aKey")
What this code does, is that it creates a sprite, initialize it with downTexture by default and:
- swap texture to upTexture immediately
- waits between 1 and 4 seconds
- swap texture to downTexture
- waits between 0.3 and 1 seconds
- repeats all this forever
If you want to stop this action, you can access it like this:
if sprite.actionForKey("aKey") != nil {
removeActionForKey("aKey")
}