【问题标题】:How to set timer for random colour in swift 2?如何在swift 2中设置随机颜色的计时器?
【发布时间】:2016-03-05 12:56:07
【问题描述】:

我想设置计时器并每 5 秒更改一次背景颜色。

我写了随机颜色的代码,它的工作很完美,但我试图把这个函数放在 NSTimer 中,我被迷住了。

2016-03-05 14:46:48.774 Boo Adventure[6782:365555] *** 终止应用 由于未捕获的异常“NSInvalidArgumentException”,原因: '-[Boo_Adventure.GameScene 更新]:无法识别的选择器发送到 实例 0x7fe2cb741ac0'

游戏场景:

extension CGFloat {
    static func random() -> CGFloat {
        return CGFloat(arc4random()) / CGFloat(UInt32.max)
    }
}


extension UIColor {
    static func randomColor() -> UIColor {
        let r = CGFloat.random()
        let g = CGFloat.random()
        let b = CGFloat.random()

        // If you wanted a random alpha, just create another
        // random number for that too.
        return UIColor(red: r, green: g, blue: b, alpha: 2.5)
    }
}

override func didMoveToView(view: SKView) {

        Timer = NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: "update", userInfo: nil, repeats: false)

        func update() {

            self.view!.backgroundColor = UIColor.randomColor()

        }
}

谢谢!

【问题讨论】:

    标签: ios sprite-kit swift2


    【解决方案1】:

    这里有几个问题:

    1) 您希望每 5 秒更改一次颜色,但您将 repeats 参数设置为 false,因此您的自定义 update() 方法将只执行一次。将repeats 更改为true

    2) 您正在尝试更改视图 (SKView) 的背景颜色,而不是场景的背景颜色。

    这是使用NSTimer 的方法:

    override func didMoveToView(view: SKView) {
          let timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "update", userInfo: nil, repeats: true)
    }
    
    func update(){
          backgroundColor = UIColor.randomColor()
    }
    

    NSTimer 不受场景或视图暂停状态的影响,因此在某些情况下可能会给您带来麻烦。为避免这种情况,您可以使用SKAction。使用 SKAction 完成的相同操作如下所示:

    override func didMoveToView(view: SKView) {
    
            let wait = SKAction.waitForDuration(5)
    
            let block = SKAction.runBlock({
                [unowned self] in
                self.backgroundColor = UIColor.randomColor()
            })
    
            let sequence = SKAction.sequence([wait,block])
    
            runAction(SKAction.repeatActionForever(sequence), withKey: "colorizing")
    
        }
    

    这样,如果您暂停场景或视图,着色操作将自动暂停(并在场景/视图取消暂停时取消暂停)。

    【讨论】:

    【解决方案2】:

    您的函数update 看起来像是在计时器的闭包中 - 但事实并非如此。把函数取出来,变成这样的样子

    override func didMoveToView(view: SKView) {
       Timer = NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: "update", userInfo: nil, repeats: false)
    }
    
    func update() {
        self.view!.backgroundColor = UIColor.randomColor()
    }
    

    【讨论】:

      【解决方案3】:

      不知道这是否能解决问题,但根据NSTimer Class Reference,选择器应该有签名:

      timerFireMethod:
      

      尝试将您的 update 函数签名更新为

      func update(timer: NSTimer)
      

      并更新这一行:

      Timer = NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: "update", userInfo: nil, repeats: false)
      

      到:

      Timer = NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: "update:", userInfo: nil, repeats: false)
      

      【讨论】:

      • 我需要在 timerFireMethod 中添加什么?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-29
      • 2015-11-07
      • 2013-06-07
      • 1970-01-01
      • 2014-11-13
      • 2019-06-04
      • 1970-01-01
      相关资源
      最近更新 更多