How can I make a function execute every second in swift?

后端 未结 7 1794
离开以前
离开以前 2020-11-29 01:59

I want to add a score to the top of my scene in the game I am working on. The score is going to based on how long you last, and will increase every second. Thanks for the he

7条回答
  •  囚心锁ツ
    2020-11-29 02:45

    Xcode 10.2 Swift 5:

    override func viewDidLoad() {
        super.viewDidLoad()
        // ...
        Timer.scheduledTimer(timeInterval: 8.0, target: self, selector: Selector(("your @obcj func name")), userInfo: nil, repeats: true)
    }
    
    //Anywhere in the same view controller to stop the loop:
    Timer.cancelPreviousPerformRequests(withTarget: your @obcj func name())
    

提交回复
热议问题