以随机间隔重复动画?

时间:2016-06-05 09:28:21

标签: ios iphone swift sprite-kit

我希望我创建一个动画,以便在用户使用该应用程序时,在每个动画之间以随机间隔重复运行。

我创建了一个完成动画的功能。

         func Yawn () {

        Yawn1.append(character_atlas.textureNamed("1.png"))
        Yawn1.append(character_atlas.textureNamed("2.png"))
        Yawn2.append(character_atlas.textureNamed("3.png"))
        Yawn2.append(character_atlas.textureNamed("4.png"))
        Character_animate = SKSpriteNode(texture: Yawn1[0])
        Character_animate2 = SKSpriteNode(texture: Yawn2[0])
        Character_animate.size = CGSize(width: self.size.width/6.3, height: self.size.height/5.6)
        Character_animate2.size = CGSize(width: self.size.width/6.3, height: self.size.height/5.6)
        Character_animate2.position = CGPoint(x: self.frame.width/2, y: self.frame.height*0.90)
        Character_animate.position = CGPoint(x: self.frame.width/2, y: self.frame.height*0.90)
        Character_animate.zPosition = 3
        Character_animate2.zPosition = 3
        addChild(Character_animate)
        addChild(Character_animate2)
        var action1 = SKAction.repeatAction(SKAction.animateWithTextures(Yawn1, timePerFrame: 0.4), count: 3)
        var action2 = SKAction.repeatAction(SKAction.animateWithTextures(Yawn2, timePerFrame: 1), count: 4)
        var action3 = SKAction.sequence([action1,action2])

        var action4 = SKAction.runBlock({self.Character_animate.removeFromParent()})
        var action5 = SKAction.runBlock({self.Character_animate2.removeFromParent()})
        var action6 = SKAction.sequence([action4,action5])

        Character_animate2.runAction(action3,completion: {
            self.runAction(action6)
        })
     }

1 个答案:

答案 0 :(得分:2)

您可以使用NSTimer并使用随机数来定义函数NSTimerInterval每次触发之间的Yawn

let randomNumber = Int(arc4random_uniform(10))  // This will return random number from 0 to 9 (10-1)
NSTimer.scheduledTimerWithTimeInterval(randomNumber, target: self, selector: #selector(YourVC.Yawn) , userInfo: nil, repeats: true)