Created
April 5, 2014 18:14
-
-
Save veeneck/9995797 to your computer and use it in GitHub Desktop.
Follow curve to point with Sprite Kit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Imagine shooting an arrow to a point | |
CGMutablePathRef path = CGPathCreateMutable(); | |
// This will move an invisible path marker to a starting point | |
// In my case, the node is a child node of a sprite, so it is the local coords and not the screen coords | |
CGPathMoveToPoint(path, NULL, 10, 0); | |
// The 3-8th parameters are x/y coords. The arrow iwll first hit x:100 and y:50. | |
// It will then raise up a bit as it keeps going and finally drop to the target at x:300 y:0 | |
CGPathAddCurveToPoint(path, NULL, | |
100, 50, | |
200, 75, | |
300, 0); | |
// Create an action based on this curve. oritentToPath will make the arrows tip point up and down in the correct spots. | |
// Be careful, it is based off of each sprite having the correct default state. (i.e.: arrows and characters are vertical by default) | |
SKAction *followCurve = [SKAction followPath:path asOffset:NO orientToPath:YES duration:3.0]; | |
// Run the action on the SKSpriteNode | |
[arrow runAction:followCurve] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment