Spritekit中的计时器和Objective-C

时间:2015-10-04 20:37:17

标签: objective-c timer

所以我试图将一个计时器加入到我的项目中,这将逐渐减少。例如,一个3秒钟的计时器在3秒后最终将为0。我在这个问题之前提出了这个问题,并得到了关于使用NSTimeInterval的答复,这就是我在代码中设置它的方式:

@implementation MyScene {
    NSTimeInterval _timestamp;
}

计时器启动时:

_timestamp = [NSDate timeIntervalSinceReferenceDate];
Check your timer in your update pass:

- (void)update:(NSTimeInterval)currentTime {
    if(_timestamp != nil && currentTime - _timestamp.timeIntervalSinceReferenceDate >= 3.0) {
        // Perform your timer event
    }

    // Other updates
}

然而,这在更新的第一行给出了一些错误:错误是:

member reference base type 'NSTimeInterval' (aka 'double') is not a structure or union

Invalid operands to binary expression ('NSTimeInterval' (aka 'double') and 'void )

1 个答案:

答案 0 :(得分:0)

_timestamp的声明更改为:

NSDate *_timestamp;

并改变:

_timestamp = [NSDate timeIntervalSinceReferenceDate];

为:

_timestamp = [NSDate date];