Search code examples
objective-cios7sprite-kitskspritenode

SKAction how to combine withKey and completion


I´m quite new to iOS and Sprite Kit programming and wonder how to combine:

-[SKAction runAction:withKey:]

and

-[SKAction runAction:completion:]

On the one side, I want to prevent a node from running the same action (or action sequence) again, on the other, I want to react to the termination of an action, but there is no method and, as far as I see, no way to use both at the same time.

Thx


Solution

  • Use +runBlock: action in +sequence: like this:

    SKAction *yourAction = ...
    
    SKAction *completion = [SKAction runBlock:^{
        // Your code.
    }];
    SKAction *sequence = [SKAction sequence:@[ yourAction, completion ]];
    
    [node runAction:sequence withKey:yourKey];
    

    If you use this multiple times, create SKNode category with such method:

    - (void)runAction:(SKAction *)action withKey:(NSString *)key completion:(void(^)(void))block;