请考虑以下递归:
- (void)addFlashActionToLampWithLampIndex:(int)index {
LampNode *lamp = (LampNode *)self.children[index];
int nextLampIndex = index + 1;
if (nextLampIndex > self.children.count - 1) {
nextLampIndex = 0;
}
SKAction *flash = [SKAction runBlock:^{
NSLog(@"2");
}];
// SKAction *wait = [SKAction waitForDuration:0.015f];
NSLog(@"1");
[self runAction:flash completion:^{
[self addFlashActionToLampWithLampIndex:nextLampIndex];
}];
}
输出:
2014-03-27 20:51:21.104 SpinNWin[15811:60b] 1
2014-03-27 20:51:21.110 SpinNWin[15811:60b] 2
2014-03-27 20:51:21.110 SpinNWin[15811:60b] 1
2014-03-27 20:51:21.143 SpinNWin[15811:60b] 2
2014-03-27 20:51:21.144 SpinNWin[15811:60b] 1
2014-03-27 20:51:21.177 SpinNWin[15811:60b] 2
2014-03-27 20:51:21.177 SpinNWin[15811:60b] 1
2014-03-27 20:51:21.210 SpinNWin[15811:60b] 2
2014-03-27 20:51:21.210 SpinNWin[15811:60b] 1
2014-03-27 20:51:21.243 SpinNWin[15811:60b] 2
我希望该方法的执行是即时的,没有任何延迟或滞后。NSLog(@"1");
但是,您可以注意到,两者之间存在NSLog(@"2");
大约 0.03 秒的延迟。
有谁知道为什么会发生这种情况以及如何避免这种情况?