문제

Please consider the following recursion:

- (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];
    }];
}

Output:

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

I'm expecting an execution of that method to be instant, without any delays or lags. However, as you can notice, there is a lag between NSLog(@"1"); and NSLog(@"2"); for about 0.03 sec.

Does anyone has ideas why this is happening and how to avoid this?

도움이 되었습니까?

해결책

Because actions need time to progress. Actions are evaluated after the SKScene's update: method runs:

enter image description here (Source: Sprite Kit Programming Guide)

Therefore if you do runAction: the action will not run until after the scene's update method. Depending on whether you run the action somewhere within the update: message chain, or in didEvaluateActions or didSimulatePhysics Sprite Kit may render one frame without having yet run the action.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top