因此,当我尝试根据分数将时间间隔变量频率更改为较低的数字时,我将此计时器设置为在称为“频率”的时间间隔变量上运行特定函数(如下所示) number 它似乎并没有改变它发射的速率它似乎同时发射即使频率被更改为较低的数字
override func didMove(to view: SKView) {
Timer.scheduledTimer(timeInterval: frequency, target: self, selector: #selector(GameScene.spawnFallingOjects), userInfo: nil, repeats: true)
}
func spawnFallingOjects() {
if (GameState.current == .playing || GameState.current == .blackstone) {
guard usingThirdEye == false else { return }
let scoreLabel = childNode(withName: "scoreLabel") as! Score
let lane = [-100, -50 , 0, 50, 100]
let duration = 3.0
switch scoreLabel.number {
case 0...50:
frequency = 6.0
print("frequency has changed: \(frequency)")
case 51...100:
frequency = 4.5
print("frequency has changed: \(frequency)")
case 101...200000:
frequency = 1.1
print("frequency has changed: \(frequency)")
default:
return
}
let randomX = lane[Int(arc4random_uniform(UInt32(lane.count)))]
let object:Object = Object()
object.createFallingObject()
object.position = CGPoint(x: CGFloat(randomX), y: self.size.height)
object.zPosition = 20000
addChild(object)
let action = SKAction.moveTo(y: -450, duration: duration)
object.run(SKAction.repeatForever(action))
}
}
当频率数字变为较低数字时,如何使计时器触发得更快?我应该在函数结束时重新创建计时器吗?
最佳答案
您实际上应该避免使用 Timer,Sprite 工具包有自己的时间功能,并且 Timer 不能很好地与它一起使用并且管理起来真的很痛苦。
相反,使用 SKAction 等待并触发:
let spawnNode = SKNode()
override func didMove(to view: SKView) {
let wait = SKAction.wait(forDuration:frequency)
let spawn = SKAction.run(spawnFallingObjects)
spawnNode.run(SKAction.repeatForever(SKAction.sequence([wait,spawn])))
addChild(spawnNode)
}
然后为了让它更快,就这样做:
switch scoreLabel.number {
case 0...50:
spawnNode.speed = 1
print("speed has changed: \(spawnNode.speed)")
case 51...100:
spawnNode.speed = 1.5
print("speed has changed: \(spawnNode.speed)")
case 101...200000:
spawnNode.speed = 2
print("speed has changed: \(spawnNode.speed)")
default:
return
}
关于swift - 在 swift spritekit 中更改定时器的时间间隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41233556/