R. Rincón
R. Rincón

Reputation: 361

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)

Upvotes: 2

Views: 4634

Answers (2)

R. Rincón
R. Rincón

Reputation: 361

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.

Upvotes: 1

hamobi
hamobi

Reputation: 8130

I feel like you're making things hard using paths and all that. You want a circle that is just a line with no color on the inside.
Try this:

let circle = SKShapeNode(circleOfRadius: 10)

circle.physicsBody = SKPhysicsBody(circleOfRadius: 10)

circle.fillColor = SKColor.clearColor()
circle.position = CGPoint(x: self.size.width/2, y: self.size.height/2)
self.addChild(circle)

Upvotes: 4

Related Questions