Swift 快速倒计时功能

Swift 快速倒计时功能,swift,sprite-kit,Swift,Sprite Kit,我正在尝试使用SpriteKit为游戏创建倒计时计时器,但每当我尝试运行倒计时()时,我的游戏就会冻结。我确信我的逻辑在这里是正确的。我不知道发生了什么事 func countDown(){ let countDownWait = SKAction.wait(forDuration: 1.0) repeat { self.run(countDownWait){ self.countDownTime -= 1

我正在尝试使用SpriteKit为游戏创建倒计时计时器,但每当我尝试运行倒计时()时,我的游戏就会冻结。我确信我的逻辑在这里是正确的。我不知道发生了什么事

func countDown(){
    let countDownWait = SKAction.wait(forDuration: 1.0)
        repeat {
            self.run(countDownWait){
                self.countDownTime -= 1
            }
        } while (self.countDownTime > 0)

        if self.countDownTime == 0{
            self.runGameOver()
        }
}

你考虑过用计时器吗。你可能需要20分钟左右的时间来完成它,但它详细介绍了计时器的启动、停止、暂停等

您可以在update func中检查所经过的时间,也可以使用SKAction来跟踪与您在代码中所做的类似的时间

let someLabel = SKLabelNode()

func countdown() {

    var offset: Double = 0

    for x in (0...10).reversed() {

        run(SKAction.wait(forDuration: offset)) {
            someLabel.text = "\(x)"

            if x == 0 {
                //do something when counter hits 0
                //self.runGameOver()
            }
            else {
                 //maybe play some sound tick file here
            }
        }
        offset += 1.0
    }
}

尝试此解决方案以运行倒计时

 var seconds = 7200
  var timer = Timer()



override func viewDidLoad() {
        super.viewDidLoad() 
         runTimer()
}

  func runTimer() {
        timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: (#selector(self.updateTimer)), userInfo: nil, repeats: true)

    }
    @objc func updateTimer() {
        if seconds < 1 {
            timer.invalidate()
            //Send alert to indicate time's up.
        } else {
            seconds -= 1
            timerLabel.text = timeString(time: TimeInterval(seconds))
        }
    }

    func timeString(time:TimeInterval) -> String {
        let hours = Int(time) / 3600
        let minutes = Int(time) / 60 % 60
        let seconds = Int(time) % 60
        return String(format:"%02i:%02i:%02i", hours, minutes, seconds)
    }
var秒=7200
var timer=timer()
重写func viewDidLoad(){
super.viewDidLoad()
运行计时器()
}
func运行计时器(){
timer=timer.scheduledTimer(时间间隔:1,目标:self,选择器:(#选择器(self.updateTimer)),userInfo:nil,repeats:true)
}
@objc func updateTimer(){
如果秒数小于1{
timer.invalidate()
//发送警报以指示时间已到。
}否则{
秒-=1
timerLabel.text=timeString(时间:时间间隔(秒))
}
}
func timeString(时间:TimeInterval)->String{
让小时数=整数(时间)/3600
分钟数=整数(时间)/60%60
设秒=整数(时间)%60
返回字符串(格式:“%02i:%02i:%02i”,小时、分钟、秒)
}

以下是我如何为我的Swift/SpriteKit“突破”应用程序解决这个问题的。我想在游戏主屏幕上从5倒计时到1倒计时,但在球开始移动之前。我添加了这些函数,然后在
didMoveToView
的末尾调用了倒计时(5):注意
ball.physicsBody!。应用脉冲(CGVectorMake(20,20))
作为
结束倒计时的最后一步,它启动球并有效地启动游戏

func countdown(count: Int) {
    countdownLabel.horizontalAlignmentMode = .Center
    countdownLabel.verticalAlignmentMode = .Baseline
    countdownLabel.position = CGPoint(x: size.width/2, y: size.height*(1/3))
    countdownLabel.fontColor = SKColor.whiteColor()
    countdownLabel.fontSize = size.height / 30
    countdownLabel.zPosition = 100
    countdownLabel.text = "Launching ball in \(count)..."

addChild(countdownLabel)

let counterDecrement = SKAction.sequence([SKAction.waitForDuration(1.0),
    SKAction.runBlock(countdownAction)])

runAction(SKAction.sequence([SKAction.repeatAction(counterDecrement, count: 5),
    SKAction.runBlock(endCountdown)]))

}

func countdownAction() {
    count--
    countdownLabel.text = "Launching ball in \(count)..."
}

func endCountdown() {
    countdownLabel.removeFromParent()
    ball.physicsBody!.applyImpulse(CGVectorMake(20, 20))
}

您没有显示所有相关的代码行。您正在阻止主线程,因此在该循环进行时不会发生任何其他情况。您的意思是什么?您如何以及从何处调用
countDown
from?可能重复的是UIKit教程而不是Spritekit教程。Spritekit在游戏循环中内置了自己的计时功能,可通过更新功能访问。Sprite Kit=使用SKActions中不使用计时器。Spritekit中不使用计时器。然后兄弟。。检查你的线程可能在主线程中运行你的循环,这将粗暴地阻止你的UI,直到它完成…SK游戏并不真正谈论主线程,或阻止UI-只是SK有自己的游戏循环,在游戏循环中,SKActions被评估和解决,计时器完全在游戏循环e之外,g如果暂停场景,所有内容也将暂停,除非运行计时器。