Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
211 views
in Technique[技术] by (71.8m points)

ios - How can I reset the timer after a certain duration?

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
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...