我怎样才能使一个函数执行每秒迅速?

我想在我正在处理的游戏中将场景添加到场景的顶部。 得分是根据你持续的时间而定的,并且会每秒增加。 我在这里先向您的帮助表示感谢!

import SpriteKit class easyScene: SKScene { let scrollBarEasyBottom = SKSpriteNode(imageNamed: "scrollBarEasyBottom") let scrollBarEasyTop = SKSpriteNode(imageNamed: "scrollBarEasyTop") let ball = SKSpriteNode(imageNamed: "ball") var origSBEBpositionX = CGFloat(0) var origSBETpositionX = CGFloat(0) var maxSBEBX = CGFloat(0) var SBEBSpeed = 5 var maxSBETX = CGFloat(0) var SBETSpeed = 5 var score = 0 var timer: NSTimer? var scoreText = SKLabelNode(fontNamed: "Kailasa") override func didMoveToView(view: SKView) { println("Easy Scene is the location") self.backgroundColor = UIColor.blackColor() self.scrollBarEasyBottom.position = CGPoint(x:0, y:270) self.addChild(self.scrollBarEasyBottom) self.scrollBarEasyBottom.yScale = 0.2 self.origSBEBpositionX = self.scrollBarEasyBottom.position.x // end scrollBarEasyBottom self.scrollBarEasyTop.position = CGPoint(x:20, y:400) self.addChild(self.scrollBarEasyTop) self.scrollBarEasyTop.yScale = 0.2 self.origSBETpositionX = self.scrollBarEasyTop.position.x // end scrollBarEasyTop self.ball.position = CGPoint(x:40, y:293) self.addChild(self.ball) self.ball.yScale = 0.17 self.ball.xScale = 0.17 // end ball self.maxSBEBX = self.scrollBarEasyBottom.size.width - self.frame.size.width self.maxSBEBX *= -1 self.maxSBETX = self.scrollBarEasyTop.size.width - self.frame.size.width self.maxSBETX *= -1 // self.scoreText.text = "0" self.scoreText.fontSize = 60 self.scoreText.position = CGPoint(x: CGRectGetMidX(self.frame), y: 500) self.scoreText.text = String(self.score) self.addChild(self.scoreText) timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("scoreIncrease") , userInfo: nil, repeats: true) func scoreIncrease (){ score++ println(score) } } override func update(currentTime: NSTimeInterval){ if self.scrollBarEasyBottom.position.x <= maxSBEBX + 1200 { self.scrollBarEasyBottom.position.x = self.origSBEBpositionX } if self.scrollBarEasyTop.position.x <= maxSBETX + 1200 { self.scrollBarEasyTop.position.x = self.origSBETpositionX } scrollBarEasyBottom.position.x -= CGFloat(self.SBEBSpeed) scrollBarEasyTop.position.x -= CGFloat(self.SBETSpeed) // moving bars var degreeRotation = CDouble(self.SBEBSpeed) * M_PI / 180 self.ball.zRotation -= CGFloat(degreeRotation) //rotate ball } } 

运行这个代码后,我总是得到一个“无法识别的select器发送到实例错误”。

你可以使用这样的一个:

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

Swift 3:

 var timer = Timer() override func viewDidLoad() { // Use for the app's interface scheduledTimerWithTimeInterval() } override func didMove(to view: SKView) { // As part of a game scheduledTimerWithTimeInterval() } func scheduledTimerWithTimeInterval(){ // Scheduling timer to Call the function "updateCounting" with the interval of 1 seconds timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.updateCounting), userInfo: nil, repeats: true) } func updateCounting(){ NSLog("counting..") } 

有一种叫做NSTimer的东西可以解决你的问题。 我已经给出了一个如何使用它的例子。 只是为了你的目的定制它。

 var timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("yourMethodToCall"), userInfo: nil, repeats: true) 

将此行添加到需要重复调​​用您的函数的地方。

  1. 1.0是指1秒。
  2. 更改selector以调用yourMethodName
  3. repeats设置为true ,每秒钟调用该函数。

试试这个,让我知道如果你卡在某个地方。 谢谢。

Swift 3

find这个解决scheme,它为我工作

 weak var timer: Timer? var timerDispatchSourceTimer : DispatchSourceTimer? func startTimer() { if #available(iOS 10.0, *) { timer = Timer.scheduledTimer(withTimeInterval: 3, repeats: true) { [weak self] _ in // do something here } } else { // Fallback on earlier versions timerDispatchSourceTimer = DispatchSource.makeTimerSource(flags: [], queue: DispatchQueue.main) timerDispatchSourceTimer?.scheduleRepeating(deadline: .now(), interval: .seconds(60)) timerDispatchSourceTimer?.setEventHandler{ // do something here } timerDispatchSourceTimer?.resume() } } func stopTimer() { timer?.invalidate() //timerDispatchSourceTimer?.suspend() // if you want to suspend timer timerDispatchSourceTimer?.cancel() } // if appropriate, make sure to stop your timer in `deinit` deinit { stopTimer() }