Search code examples
swiftellipseskactioncgpath

how to set starting point (or starting angle) of a path described by CGPathCreateWithEllipseInRect?


I have a sprite that follows a path described by CGPathCreateWithEllipseInRect. It always start at the right-most point (I guess because the default is to start at angle = 0). How do I make it start from the top-most point (at angle = π/2) ? My current code is like this:

    let pathCenter = CGPoint(x: frame.width/2 , y: frame.height/2)
    let pathDiameter = CGFloat(frame.height/4)
    let path = CGPathCreateWithEllipseInRect(CGRect(origin: pathCenter, size: CGSize(width: pathDiameter * 1.5, height: pathDiameter * 0.8)), nil)
    let followPath = SKAction.followPath(path, asOffset: false, orientToPath: false, duration: 6.0)
    sprite.runAction(SKAction.repeatActionForever(followPath))

Solution

  • You'd have to draw your own path instead of using CGPathCreateWithEllipseInRect, adding points in an order that corresponds to where you want the sprite to start.

    Try the method in this post: How to draw a circle starting at the top, and then modify as needed.