Swift 计时器 SK.wait

Swift Timer SK.wait

我让球每 ​​3 秒生成一次,我希望球在分数增加时生成得更快。问题是我的生成率在 4 秒内保持不变,并且在我的分数增加时不会改变。感谢帮助 m8s

  var spawnRate : TimeInterval = 4

    var  wait = SKAction .wait(forDuration: spawnRate, withRange: 0.2)

    if score >= 6  {
        spawnRate = 3
        wait = SKAction .wait(forDuration: spawnRate, withRange: 0.2)
    }
     let spawning = SKAction.sequence([wait,spawn])

我宁愿重构代码,仔细检查分数是否真的增加了,并取消之前的操作,以防它们在上次发布后还没有完成。我假设你产生了几个动作,它们在未来 "accumulated"。

        var spawnRate : TimeInterval
        var wait:SKAction!

        if score >= 6  {
            spawnRate = 3
            wait = SKAction .wait(forDuration: spawnRate, withRange: 0.2)
        }
        else {
            spawnRate = 4
            wait = SKAction .wait(forDuration: spawnRate, withRange: 0.2)
        }

        let spawningKey ="spawningKey"
        self.spinnyNode?.removeAction(forKey: waitKey)

        let spawning = SKAction.sequence([wait,spawn])

        self.spinnyNode?.run(spawning, withKey: spawningKey)