1

My SKSpriteNode follows this path in a counterclockwise direction, how do i make it so it follows the path in a clockwise direction?

Thanks in advance!

    CGPathRef circle = CGPathCreateWithEllipseInRect(CGRectMake(0,0,50,50), NULL);

    SKAction *followTrack = [SKAction followPath:circle asOffset:YES orientToPath:YES duration:5.0];

    SKAction *forever = [SKAction repeatActionForever:followTrack];

    [player2 runAction:forever];
user1216855
  • 275
  • 1
  • 4
  • 12

2 Answers2

7

Instead of CGPathCreateWithEllipseInRect(), use CGPathCreateMutable() and CGPathAddArc() to create the circle path and use the clockwise parameter to control the direction.

Untested code:

CGMutablePathRef circle = CGPathCreateMutable();
CGPathAddArc(circle, NULL, 25, 25, 25, 0, 2*M_PI, true);
CGPathCloseSubpath(circle);
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • 1
    @user1216855: See updated answer, I hope that it works! – Martin R Mar 30 '14 at 17:43
  • 1
    Thanks Martin, I have run into a problem though trying to use this in a SKAction repeating forever. After the initial path is completed, the SKSpriteNode's position gets altered, moving up/right a few pixels each iteration. Any ideas? – Derek Hewitt Jun 18 '14 at 08:14
  • @DerekH: Sorry, I have no idea. – Martin R Jun 18 '14 at 09:19
0

From Martin's answer a few changes cause the object to start and end at the same place:

In Objective C

CGMutablePathRef circle = CGPathCreateMutable();
CGPathAddArc(circle, NULL, -25, 0, 25, 0, 2*M_PI, true);
CGPathCloseSubpath(circle);

In swift

let path = CGPathCreateMutable()
CGPathAddArc(path, nil, -25, 0, 25, 0, CGFloat(2.0 * M_PI), true)
CGPathCloseSubpath(path)
Dmitri Fuerle
  • 257
  • 2
  • 7