ios – 如何在swift中每秒执行一个功能?

ios – 如何在swift中每秒执行一个功能?,第1张

概述我想在我正在开发的游戏中添加一个分数到我的场景顶部.得分将取决于你持续多久,并将每秒增加一次.我在这里先向您的帮助表示感谢! import SpriteKitclass easyScene: SKScene {let scrollBarEasyBottom = SKSpriteNode(imageNamed: "scrollBarEasyBottom")let scrollBarEasyT 我想在我正在开发的游戏中添加一个分数到我的场景顶部.得分将取决于你持续多久,并将每秒增加一次.我在这里先向您的帮助表示感谢!
import SpriteKitclass 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 = 5var maxSBETX = CGfloat(0)var SBETSpeed = 5var score = 0var 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    }    }

运行此代码之后,我总是得到一个“无法识别的选择器发送到实例错误”.

解决方法 你可以使用这样的一个
var timer = NSTimer()overrIDe func vIEwDIDLoad() {        scheduledTimerWithTimeInterval()}func scheduledTimerWithTimeInterval(){        // Scheduling timer to Call the function **Countdown** with the interval of 1 seconds        timer = NSTimer.scheduledTimerWithTimeInterval(1,selector: Selector("updateCounting"),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 **Countdown** with the interval of 1 seconds    timer = Timer.scheduledTimer(timeInterval: 1,selector: #selector(self.updateCounting),repeats: true)}func updateCounting(){    NSLog("counting..")}
总结

以上是内存溢出为你收集整理的ios – 如何在swift中每秒执行一个功能?全部内容,希望文章能够帮你解决ios – 如何在swift中每秒执行一个功能?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1113495.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存