How can I reset the timer after a certain duration

2019-09-23 17:06发布

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
}

1条回答
趁早两清
2楼-- · 2019-09-23 17:51

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 })
    }
}
查看更多
登录 后发表回答