[英]How to create a timer in Spritekit?
Ive figured out how to make a timer in a single-view application, but not Spritekit. 我已经想出如何在单视图应用程序中制作计时器,但不是Spritekit。 When I use the following code, I get 2 errors(listed below). 当我使用以下代码时,我得到2个错误(如下所列)。 Can anyone help me out with this? 任何人都可以帮我解决这个问题吗? Thanks, Jack. 谢谢,杰克。
The timer. 计时器。
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];
The errors 错误
Incompatible pointer types assigned to 'SKLabelNode*' from 'NSTimer*'
Undeclared selector 'Timercount'
In Swift usable: 在Swift中可用:
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. 在Sprite Kit中不要使用NSTimer, GCD or performSelector:afterDelay:
因为这些计时方法忽略了节点,场景或视图的暂停状态。 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])))
You are assigning the NSTimer
you created to an SKLabelNode
. 您正在将您创建的NSTimer
分配给SKLabelNode
。 To fix your first error, change the last line to: 要修复第一个错误,请将最后一行更改为:
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(Timercount) userInfo:nil repeats:YES];
You are getting the second error because you are setting the timer to call a method called Timercount
, but you don't have one. 您收到第二个错误,因为您正在设置计时器以调用名为Timercount
的方法,但您没有。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.