Ios 如何在Spritekit中创建计时器?

Ios 如何在Spritekit中创建计时器?,ios,objective-c,sprite-kit,nstimer,Ios,Objective C,Sprite Kit,Nstimer,我知道如何在单视图应用程序中创建计时器,但没有想到Spritekit。当我使用下面的代码时,我得到2个错误(如下所列)。有人能帮我解决这个问题吗?谢谢,杰克 计时器 if(!_scorelabel) { _scorelabel = [SKLabelNode labelNodeWithFontNamed:@"Courier-Bold"]; _scorelabel.fontSize = 200; _scorelabel.position = CGPointMake(CGRe

我知道如何在单视图应用程序中创建计时器,但没有想到Spritekit。当我使用下面的代码时,我得到2个错误(如下所列)。有人能帮我解决这个问题吗?谢谢,杰克

计时器

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'

您正在将创建的
NSTimer
分配给
SKLabelNode
。要修复第一个错误,请将最后一行更改为:

[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(Timercount) userInfo:nil repeats:YES];
出现第二个错误是因为您正在将计时器设置为调用名为
Timercount
的方法,但您没有该方法。

在Swift中:

在Sprite工具包中,不要使用
NSTimer、GCD或PerformSelect:afterDelay:
,因为这些计时方法会忽略节点、场景或视图的暂停状态。此外,您不知道它们在游戏循环中的哪一点执行,这可能会导致各种问题,具体取决于您的代码实际执行了什么

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])))

NSTimer的另一种替代方法是使用SKActions。SKAction*wait=[SKAction waitForDuration:1.0f];SKAction*序列=[SKAction序列:@[[SKAction性能选择器:@selector(方法:)onTarget:self],wait]];SKAction*repeat=[SKAction repeatActionForever:sequence];[自动运行:重复];-(void)方法{…}不仅是一种替代方法,而且是一种实现方法。例如,如果游戏/场景/节点暂停,NSTimer不会停止启动。@LearnCos2D我假设OP已经设置好了管理/停止计时器的所有内容。不要使用NSTimer!见: