如何在 Spritekit 中创建计时器? [英] How to create a timer in Spritekit?
问题描述
我已经找到了如何在单视图应用程序中创建计时器的方法,但没有找到 Spritekit.当我使用以下代码时,出现 2 个错误(如下所列).谁能帮我解决这个问题?谢谢,杰克.
Ive figured out how to make a timer in a single-view application, but not Spritekit. When I use the following code, I get 2 errors(listed below). Can anyone help me out with this? Thanks, Jack.
计时器.
if(!_scorelabel) {
_scorelabel = [SKLabelNode labelNodeWithFontNamed:@"Courier-Bold"];
_scorelabel.fontSize = 200;
_scorelabel.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
_scorelabel.fontColor = [SKColor colorWithHue: 0 saturation: 0 brightness: 1 alpha: .5];
[self addChild:_scorelabel];
}
_scorelabel = [NSTimer scheduledTimerWithTimeInterval: 1 target:self selector:@selector(Timercount)userInfo: nil repeats: YES];
错误
Incompatible pointer types assigned to 'SKLabelNode*' from 'NSTimer*'
Undeclared selector 'Timercount'
推荐答案
在 Swift 中可用:
In Swift usable:
在 Sprite Kit 中不要使用 NSTimer、GCD 或 performSelector:afterDelay:
因为这些计时方法会忽略节点、场景或视图的暂停状态.此外,您不知道它们是在游戏循环中的哪个点执行的,这可能会导致各种问题,具体取决于您的代码实际执行的操作.
In Sprite Kit do not use NSTimer, GCD or performSelector:afterDelay:
because these timing methods ignore a node's, scene's or the view's paused state. Moreover you do not know at which point in the game loop they are executed which can cause a variety of issues depending on what your code actually does.
var actionwait = SKAction.waitForDuration(0.5)
var timesecond = Int()
var actionrun = SKAction.runBlock({
timescore++
timesecond++
if timesecond == 60 {timesecond = 0}
scoreLabel.text = "Score Time: \(timescore/60):\(timesecond)"
})
scoreLabel.runAction(SKAction.repeatActionForever(SKAction.sequence([actionwait,actionrun])))
这篇关于如何在 Spritekit 中创建计时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!