如何在一定时间后重置计时器?

时间:2017-03-24 19:37:35

标签: ios timer swift3 sprite-kit

我在下面的代码中设置了一个计时器。如何在每次达到给定的持续时间(例如7秒)时重置计时器?

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
}

1 个答案:

答案 0 :(得分:1)

我可能会这样做而不是

我添加了一个Cloud的子类来向您展示他们的Timer如何受到场景更新的影响,它只是以均匀的速度在场景中滚动云,无论设备性能如何

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 })
    }
}