如果我在更新方法中实现了该方法,我想以秒为单位进行计数,它仅适用于第一秒,然后它会不停地打印数字,而无需在打印下一个数字之前等待一秒钟; 我不明白为什么会发生这种情况
-(void) countinSeconds{
SKAction *count = [SKAction waitForDuration:1];
SKAction *time = [SKAction runBlock:^{
timex++;
}];
SKAction *print = [SKAction runBlock:^{
NSLog(@"%i",timex);
}];
[self runAction:[SKAction sequence:@[count,time,print]]];
}
-(void)update:(NSTimeInterval)currentTime {
[self countinSeconds];
}
最佳答案
不需要为此使用操作; update:
方法传递当前时间,因此只需记住您何时开始并做一些简单的数学运算即可:
@implementation YourClass () {
BOOL _started;
NSTimeInterval _startTime;
NSUInteger _lastLogTime;
}
@end
@implementation YourClass
- (void)update:(NSTimeInterval)currentTime
{
if (!_started) {
_startTime = currentTime;
_started = YES;
} else {
NSUInteger elapsedTime = (NSUInteger)(currentTime - _startTime);
if (elapsedTime != _lastLogTime) {
NSLog(@"Tick: %lu", elapsedTime);
_lastLogTime = elapsedTime;
}
}
}
@end
关于ios - SpriteKit 中的更新方法无法正确更新时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32566420/