I would probably do it like this instead
I've added a subclass of Clouds to show you how their Timer is affected by the update of the Scene, it just scrolls clouds across the scene at at an even pace regardless of device performance
private var lastUpdateTime: TimeInterval = 0.0
override func update(_ currentTime: TimeInterval) {
//check if game is actually playing, don't want to update time if game is paused
if lastUpdateTime == 0 || gameState != .playing {
lastUpdateTime = currentTime
return
}
let delta = currentTime - lastUpdateTime
if delta > 7 {
//this has now been 7 seconds so do something here
}
//reset timer
lastUpdateTime = currentTime
enumerateChildNodes(withName: "cloud*", using: { cloud, stop in
if let cloud = cloud as? Cloud {
cloud.update(delta: delta)
}
})
}
class Cloud: SKSpriteNode {
private var minY: CGFloat = 0
private var maxY: CGFloat = 0
private var cloudWidth: CGFloat = 0
private override init(texture: SKTexture?, color: UIColor, size: CGSize) {
super.init(texture: texture, color: color, size: size)
}
convenience init() {
let texture = SKTexture(image: #imageLiteral(resourceName: "cloud1"))
self.init(texture: texture, color: SKColor.white, size: texture.size())
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
func setup(type: CloudType) {
texture = type.texture
size = (texture?.size())!
setScale(2.0)
var midHeight = gameModel.gameHeight / 2
if let anchorY = (self.parent as? SKScene)?.anchorPoint.y {
midHeight = gameModel.gameHeight * anchorY
}
self.minY = gameModel.gameHeight * 0.3 - midHeight
self.maxY = gameModel.gameHeight * 0.9 - midHeight
cloudWidth = self.size.width
let randomY = RandomFloatBetween(min: minY, max: maxY)
self.position = CGPoint(x: gameModel.gameWidth / 2 + cloudWidth, y: randomY)
}
func update(delta: TimeInterval) {
let speedX = CGFloat(delta) * 90
self.position.x -= speedX
if self.position.x <= 0 - (gameModel.gameWidth / 2 + cloudWidth) {
resetCloud()
}
}
private func resetCloud() {
self.position.x = gameModel.gameWidth / 2 + cloudWidth
self.position.y = RandomFloatBetween(min: minY, max: maxY)
enabled = false
run(.wait(forDuration: 5, withRange: 3), completion: { self.enabled = true })
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…