SKAction followPath, creating the path

1.4k views Asked by At

I am using a simple A* Pathfinding algorithm to create a path between two points (the green and red circles below), each square is an SKSpriteNode (with a CGPoint [x,y] position). I want to animate another SKSpriteNode along a smooth path that passes through each point.

My thinking was that I could use the SKAction -followPath:duration: to do this but I am having trouble working out how to create/specify the CGPath. Any help would be much appreciated.

enter image description here

1

There are 1 answers

7
Dobroćudni Tapir On BEST ANSWER

You can create a simple CGPath like this

CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, 0, 0);
CGPathAddLineToPoint(path, NULL, 100, 0);
CGPathAddLineToPoint(path, NULL, 100, 100);

Depending on what you want to achieve you can add different elements to your path like arcs, curves, rects... You can find more about it here.