【问题标题】:Repeated animation at random intervals?以随机间隔重复动画?
【发布时间】:2016-06-05 09:28:20
【问题描述】:

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

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

         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)
        })
     }

【问题讨论】:

    标签: ios iphone swift sprite-kit


    【解决方案1】:

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

    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)
    

    【讨论】:

    • 我应该把它放在哪里?我写了这个 NSTimer.scheduledTimerWithTimeInterval(60.0, target: self, selector: Selector("Yawn"), userInfo: nil, repeats: true)
    • 我把它放在 viewdidload 中,即使时间不变,动画也开始重叠。
    • 重叠是什么意思?
    • 动画之间的时间间隔不断减少。动画看起来比平时花费的时间更长。
    • 您是否记录了对函数 Yawn() 的调用,以便检查它是否每 60 秒调用一次?
    猜你喜欢
    • 2021-02-08
    • 2012-09-13
    • 1970-01-01
    • 2021-09-25
    • 1970-01-01
    • 2016-03-30
    • 2019-04-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多