我在延迟游戏中电脑的移动时遇到问题。
我找到了一些解决方案,但在我的情况下它们不起作用,例如
var delay = NSTimer.scheduledTimerWithTimeInterval(4, target: self, selector: nil, userInfo: nil, repeats: false)
我试图将此与功能一起使用,fire但也没有任何效果。
fire
还有其他可能性吗?
迅捷3
使用GCD:
let delayInSeconds = 4.0 DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + delayInSeconds) { // here code perfomed with delay }
或使用计时器:
func myPerformeCode() { // here code to perform } let myTimer : Timer = Timer.scheduledTimer(timeInterval: 4, target: self, selector: #selector(self.myPerformeCode), userInfo: nil, repeats: false)
迅捷2
let seconds = 4.0 let delay = seconds * Double(NSEC_PER_SEC) // nanoseconds per seconds let dispatchTime = dispatch_time(DISPATCH_TIME_NOW, Int64(delay)) dispatch_after(dispatchTime, dispatch_get_main_queue(), { // here code perfomed with delay })
func myPerformeCode(timer : NSTimer) { // here code to perform } let myTimer : NSTimer = NSTimer.scheduledTimerWithTimeInterval(4, target: self, selector: Selector("myPerformeCode:"), userInfo: nil, repeats: false)