Search code examples
iosswiftsprite-kituibezierpathskspritenode

How to create a SKSpriteNode that is the circumference of a circle, just the outline?


I want to create an SKSpriteNode as described above. I have tried UIBezierPath, but XCode did not recognize the functions for creating an object of that type. I think I may have to import something, but I'm unsure as to what. When I implement the following,
I get the error: Missing argument in call: center

However, once I add the appropriate argument,
I get the error: Extra argument 'edgeLoopFromPath' in call.
My code is below:

let circlePath: UIBezierPath = UIBezierPath(arcCenter: CGPointMake(self.frame.midX, self.frame.midY), radius: self.frame.height / 2, startAngle: 0, endAngle: 360, clockwise: true)
let confinCircle: SKPhysicsBody = SKPhysicsBody(center: CGPointMake(self.frame.midX, self.frame.midY), edgeLoopFromPath: circlePath)

Solution

  • Here's my solution:

     let circlePath: UIBezierPath = UIBezierPath(arcCenter: CGPointMake(self.frame.midX, self.frame.midY), radius: self.frame.height / 2, startAngle: 0, endAngle: 360, clockwise: true)
     confiningCircle.physicsBody = SKPhysicsBody(edgeChainFromPath: circlePath.CGPath)
     self.addChild(confiningCircle)
    

    Interestingly, the name of the parameter "edgeChainFromPath" differs from the name of the parameter Apple lists on their official class reference: On the website, they call it bodyWithEdgeChainFromPath, which is different from the working code above.