SpriteKit如何在每次VARYING随机数量的动画循环后重复SKAction?

时间:2015-12-02 19:51:29

标签: ios animation sprite-kit tvos skaction

我有一个带纹理动画的SKSpriteNode。我基本上有一个4帧的字符空闲周期和一个闪烁动画序列。我想永远循环字符空闲周期,但让它以随机间隔播放闪烁动画序列。

我有以下代码;

func playIdle() {

    let idle_loop = SKAction.repeatAction(action_textureSequence_idle!, count: randomLoopCount())    

    let sequence = SKAction.sequence([idle_loop, action_textureSequence_blink!])

    let repeatSequence = SKAction.repeatActionForever(sequence)

    runAction(repeatSequence)

}

func randomLoopCount() -> Int {

    return Int(arc4random_uniform(10) + 2)

}

obove的问题是,随机数只生成一次,因此眨眼根本不会随机发生。在每次循环次数相同之后。我如何达到我正在寻找的效果?

1 个答案:

答案 0 :(得分:2)

您可以使用递归来实现您想要的效果:

func playIdle() {

        let idle_loop = SKAction.repeatAction(action_textureSequence_idle, count: Int(arc4random_uniform(10) + 2))

        let sequence = SKAction.sequence([idle_loop,
                                            action_textureSequence_blink,
                                            SKAction.runBlock({[unowned self] in self.playIdle()})])

        runAction(sequence)

    }

部分unowned self可以防止您创建strong reference cycle