SpriteKit如何在每次随机变化动画循环后重复SKAction? [英] SpriteKit how to repeat an SKAction after a VARYING random number of animation loops each time?

查看:160
本文介绍了SpriteKit如何在每次随机变化动画循环后重复SKAction?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有纹理动画的SKSpriteNode。我基本上有一个4帧的角色空闲周期和一个眨眼动画序列。我想永久循环角色空闲周期,但让它以随机间隔播放眨眼动画序列。

I have an SKSpriteNode with texture animations. I basically have a character idle cycle of 4 frames and a blink animation sequence. I want to loop the character idle cycle forever but make it play the blink animation sequence at random intervals.

我有以下代码;

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)

}

圆规的问题在于,随机数仅生成一次,因此眨眼根本不会随机发生。每次循环次数相同之后。如何达到我想要的效果?

The problem with the obove is, the random number is only generated once so the blink does not happen randomly at all. Just after the same number of loops each time. How do I achieve the effect I'm looking for?

推荐答案

您可以使用递归来实现所需的目标:

You can use recursion to achieve what you want:

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)

    }

无名的自我部分可保护您避免创建强大的参考周期

The part unowned self protects you from creating a strong reference cycle.

这篇关于SpriteKit如何在每次随机变化动画循环后重复SKAction?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆