如何在一段时间后重置计时器? [英] How can I reset the timer after a certain duration?
本文介绍了如何在一段时间后重置计时器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在下面的代码中设置了一个计时器.如何在每次达到给定的持续时间(例如 7 秒)时重置计时器?
I have set a timer in my code below. How can I make the timer reset every time it reaches a given duration say, 7 seconds?
class SpriteGroup {
var sprites : [Sprite]
var isVisible : Bool
var startTime: TimeInterval = 0.0
var currentTime: TimeInterval = 0.0
init(sprites : [Sprite], isVisible: Bool, pos: CGPoint) {
self.sprites = sprites
...
startTime = Date.timeIntervalSinceReferenceDate
Timer.scheduledTimer(timeInterval: 1,
target: self,
selector: #selector(self.advanceTimer(timer:)),
userInfo: nil,
repeats: true)
}
func advanceTimer(timer: Timer)
{
currentTime = Date.timeIntervalSinceReferenceDate - startTime
}
推荐答案
我可能会这样做
我添加了一个 Clouds 的子类来向您展示它们的 Timer 如何受到场景更新的影响,它只是以均匀的速度在场景中滚动云,而不管设备性能如何
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 })
}
}
这篇关于如何在一段时间后重置计时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文