How do I get a callback from a SpriteKit repeating action when the sprite reaches the end of a path?

StackOverflow https://stackoverflow.com/questions/19670590

質問

I have created my SKAction in this manner:

unicornAction = [SKAction followPath:mypath asOffset:NO orientToPath:YES duration:0.1];

and added it to my SKSprite:

[sprite runAction:[SKAction repeatActionForever:unicornAction] withKey:@"move"];

I do this so that I can adjust the speed at any time within the sprites motion across the path.

When my sprite gets to the end of the path, I need a callback so that I can remove the sprite. How can I get such a callback?

Also, is there a better way of using SKAction to do what I am trying to do, while allowing me to change the speed anywhere during the actions run?

役に立ちましたか?

解決

You can use a sequence with a runBlock or performSelector at the end:

SKAction* sequence = [SKAction sequence:@[unicornAction, [SKAction runBlock:^{
    // code at end of path goes here...
}]];

You can also use

[sprite runAction:sequence withKey:@"follow path"];

and later get the action by key:

SKAction* sequence = [sprite actionForKey:@"follow path"];

他のヒント

Create a second runBlock: action that does whatever you want it to do when the action finishes, then create a sequence: action with your followPath and block finishing actions. If you don't want to use a block there's also a performSelector:onTarget: action.

https://developer.apple.com/library/ios/documentation/SpriteKit/Reference/SKAction_Ref/Reference/Reference.html

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top