如何让函数每秒快速执行一次? [英] How can I make a function execute every second in swift?

查看:18
本文介绍了如何让函数每秒快速执行一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我正在制作的游戏中的场景顶部添加一个分数.分数将根据你持续多长时间,并且每秒都会增加.提前感谢您的帮助!

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 help in advance!

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

运行这段代码后,我总是得到一个

After running this code, I always get an

无法识别的选择器发送到实例错误

unrecognized selector sent to instance error

推荐答案

你可以这样使用:

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


斯威夫特 5:

注意:此方案兼容 iOS 10.0+.

Note: this solution is compatible with iOS 10.0+.

// If needing to check for iOS compatibility use
// if #available(iOS 10.0, *) {code}

var timer = Timer()

override func viewDidLoad() {
    self.timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true, block: { _ in
        updateCounting()
    })
}

func updateCounting(){
    print("counting...")
}

然后您可以使用以下方法使计时器无效(停止):

You can then invalidate (stop) the timer using:

timer.invalidate()

这篇关于如何让函数每秒快速执行一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆