简体   繁体   English

Swift-游戏计时器倒计时

[英]Swift - Timer Countdown for gameplay

I have looked online at many different 'Timer' tutorials, but they are over-complicated with things like backgrounds etc What I would like is a timer at the top of the screen that counts down '3, 2, 1, Play'. 我在网上看过许多不同的“计时器”教程,但是它们与诸如背景之类的东西过于复杂,我想要的是屏幕顶部的计时器,其倒数为“ 3、2、1,播放”。 I will have images instead of text. 我将使用图片而不是文字。 This timer is activated as soon as a touch - to - begin button is clicked (which I have already made). 一旦单击“开始”按钮(我已经完成),就会激活此计时器。 Any help you can give me to do this in Apple Swift would be very greatly appreciated. 您在Apple Swift中为我提供的任何帮助将不胜感激。

Did you try the method scheduledTimerWithTimeInterval from NSTimer 您是否尝试过NSTimer scheduledTimerWithTimeInterval方法

// IBOutlet From Storyboard
@IBOutlet weak var imgView: UIImageView!

var countdown : Int! = 0   // Variable to check the count down seconds

override func viewDidLoad() {
      // Scheduling timer to Call the function **Countdown** with the interval of 1 seconds
      NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("countDown:"), userInfo: nil, repeats: true)
 }

 func countDown(timer: NSTimer) {
    countdown = countdown + 1   
    if (countdown == 1) {       // second 1, show image 3 
        imgView.image = UIImage(named: "three.png")
    }
    else if (countdown == 2) {  // second 2, show image 2
        imgView.image = UIImage(named: "two.png")
    }
    else if (countdown == 3) {  // second 3, show image 1
        imgView.image = UIImage(named: "one.png")
    }
    else if (countdown == 4) {  // second 4, show image 'play'
        imgView.image = UIImage(named: "play.png")            
    }
    else {
    // Invalidate the timer & remove the `time image` and continue with your `gameplay`
        imgView.removeFromSuperview()
        timer.invalidate()
    }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM