如何在swift中每秒执行一次函数?

时间:2015-05-07 01:16:34

标签: ios swift sprite-kit

我想在我正在制作的游戏中添加一个得分到我的场景顶部。分数将根据您的持续时间而定,并且每秒都会增加。感谢您的帮助!

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


    }


    }

运行此代码后,我总是会收到“无法识别的选择器发送到实例错误”。

7 个答案:

答案 0 :(得分:67)

您可以使用以下内容:

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..")
}

斯威夫特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)
}

@objc func updateCounting(){
    NSLog("counting..")
}

答案 1 :(得分:27)

swift中有一些名为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以每秒调用该函数。
  4. 尝试一下,如果你被困在某个地方,请告诉我。感谢。

答案 2 :(得分:8)

Swift 3

找到适合我的解决方案

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()
}

答案 3 :(得分:2)

我认为您不需要NSTimer

由于您使用的是SpriteKit,因此我将建议最简单的解决方案:

声明变量var prevScoreCalcTime:TimeInterval = 0

update的{​​{1}}函数内部,如下设置:

GameScene

祝你好运!

答案 4 :(得分:0)

//每秒运行一段代码

///Runs every second, to cancel use: timer.invalidate()
@discardableResult public static func runThisEvery(
    seconds: TimeInterval,
    startAfterSeconds: TimeInterval,
    handler: @escaping (CFRunLoopTimer?) -> Void) -> Timer {
    let fireDate = startAfterSeconds + CFAbsoluteTimeGetCurrent()
    let timer = CFRunLoopTimerCreateWithHandler(kCFAllocatorDefault, fireDate, seconds, 0, 0, handler)
    CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer, CFRunLoopMode.commonModes)
    return timer!
}

答案 5 :(得分:0)

我更喜欢

if timer != nil
{
    timer?.invalidate()
    timer = nil
}

//Your params

timer =  Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { (timer) in

          //use your params

}

答案 6 :(得分:0)

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)

}// end of viewDidLoad


//Anywhere in the same view controller to stop the loop:
Timer.cancelPreviousPerformRequests(withTarget: your @obcj func name())