首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >随机间隔重复动画?

随机间隔重复动画?
EN

Stack Overflow用户
提问于 2016-06-05 09:28:20
回答 1查看 154关注 0票数 1

我希望有一个动画,我创建的重复运行之间的每一个动画之间的随机间隔,而用户正在使用应用程序。

我创建了一个做动画的函数。

代码语言:javascript
复制
         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)
        })
     }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-06-05 11:00:17

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

代码语言:javascript
复制
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)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37640380

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档