How to draw a circle in swift using spriteKit? How to draw a circle in swift using spriteKit? swift swift

How to draw a circle in swift using spriteKit?


screenshot

If you just want to draw one simple circle at some point it's this:

func oneLittleCircle(){    var Circle = SKShapeNode(circleOfRadius: 100 ) // Size of Circle    Circle.position = CGPointMake(frame.midX, frame.midY)  //Middle of Screen    Circle.strokeColor = SKColor.blackColor()    Circle.glowWidth = 1.0    Circle.fillColor = SKColor.orangeColor()    self.addChild(Circle)}

The below code draws a circle where the user touches. You can replace the default iOS SpriteKit Project "GameScene.swift" code with the below.

screenshot

//// Draw Circle At Touch .swift// Replace GameScene.swift in the Default SpriteKit Project, with this code.import SpriteKitclass GameScene: SKScene {override func didMoveToView(view: SKView) {    /* Setup your scene here */    scene?.backgroundColor = SKColor.whiteColor()  //background color to white}override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {    /* Called when a touch begins */    for touch: AnyObject in touches {        let location = touch.locationInNode(self)        makeCirlceInPosition(location)  // Call makeCircleInPostion, send touch location.    }}//  Where the Magic Happens!func makeCirlceInPosition(location: CGPoint){    var Circle = SKShapeNode(circleOfRadius: 70 ) // Size of Circle = Radius setting.    Circle.position = location  //touch location passed from touchesBegan.    Circle.name = "defaultCircle"    Circle.strokeColor = SKColor.blackColor()    Circle.glowWidth = 1.0    Circle.fillColor = SKColor.clearColor()    self.addChild(Circle)}  override func update(currentTime: CFTimeInterval) {    /* Called before each frame is rendered */}}


Swift

let circle = SKShapeNode(circleOfRadius: 100 ) // Create circlecircle.position = CGPoint(x: 0, y: 0)  // Center (given scene anchor point is 0.5 for x&y)circle.strokeColor = SKColor.blackcircle.glowWidth = 1.0circle.fillColor = SKColor.orangeaddChild(circle)


I check your code it works but what maybe happening is the ball disappears because you have dynamic as true. turn it off and try again. the ball is dropping out of the scene.

 var Circle = SKShapeNode(circleOfRadius: 40)    Circle.position = CGPointMake(500, 500)    Circle.name = "defaultCircle"    Circle.strokeColor = SKColor.blackColor()    Circle.glowWidth = 10.0    Circle.fillColor = SKColor.yellowColor()    Circle.physicsBody = SKPhysicsBody(circleOfRadius: 40)    Circle.physicsBody?.dynamic = false //set to false so it doesn't fall off scene.    self.addChild(Circle)