How to draw a circle in swift using spriteKit?
Asked Answered
S

5

29

I just started doing ios development with swift and I can't figure out how to draw a circle. I'm just trying to draw a circle, set it to a variable and display in on the screen so I can later use it as the main player. Could anyone tell me how to do this or provide me with the code for this?

I found this code online:

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 = true //.physicsBody?.dynamic = true
self.addChild(Circle)

but when I put this on xcode and run the application nothing comes up in the game scene.

Shipowner answered 4/11, 2014 at 4:27 Comment(3)
At what point have you called the code?Vesica
If you are running the code on the iPhone simulator you cannot see the circle as it's position is out of the view's bounds. Try using circle.position = CGPointMake(100,100) or some other valueVesica
Thank you ZeMoon that was a silly mistake that I didn't notice. I set it to 100,100 and now I can see it. Thanks again. Also thanks 0x141E!Shipowner
C
39

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 SpriteKit

class 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 */
}}
Cogen answered 28/3, 2015 at 16:58 Comment(1)
Ideally, you don't want to use CGPointMake. Using CGPoint directly is preferred in Swift.Ploughshare
M
6

Swift

let circle = SKShapeNode(circleOfRadius: 100 ) // Create circle
circle.position = CGPoint(x: 0, y: 0)  // Center (given scene anchor point is 0.5 for x&y)
circle.strokeColor = SKColor.black
circle.glowWidth = 1.0
circle.fillColor = SKColor.orange
addChild(circle)
Montpellier answered 23/4, 2017 at 21:28 Comment(0)
H
3

In modern Swift(Xcode 13.2.1 and Swift 5.1), try using apple's default game template when creating a project. Then get rid of the actions file and replace the contents of the class GameScene in gameScene.swift with this:

override func didMove(to view: SKView) {
    let Circle = SKShapeNode(circleOfRadius: 40)
    Circle.position = CGPoint(x: 100, y: 100)
    Circle.name = "defaultCircle"
    Circle.strokeColor = SKColor.black
    Circle.glowWidth = 10.0
    Circle.fillColor = SKColor.yellow
    Circle.physicsBody = SKPhysicsBody(circleOfRadius: 40)
    Circle.physicsBody?.isDynamic = false 
    self.addChild(Circle)
}
Hawks answered 4/1, 2022 at 0:33 Comment(1)
This is terrible advice, making someone create a whole new project just to place that one bit of code. And what If we're not making a game but are using SpriteKit for other things. This is just wrongTheorize
D
2

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)
Dorcus answered 23/12, 2014 at 22:8 Comment(0)
I
1

try this

var circle : CGRect = CGRectMake(100.0, 100.0, 80.0, 80.0) //set your dimension
var shapeNode : SKShapeNode = SKShapeNode()
shapeNode.path = UIBezierPath.bezierPathWithOvalInRect(circle.CGPath)
shapeNode.fillColor = SKColor.redColor()
shapeNode.lineWidth = 1 //set your border
self.addChild(shapeNode)
Imaginative answered 4/11, 2014 at 16:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.