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.

TheNeil
  • 3,321
  • 2
  • 27
  • 52
elpita
  • 1,085
  • 3
  • 12
  • 13
  • 1
    At what point have you called the code? – ZeMoon Nov 04 '14 at 06:16
  • 2
    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 value – ZeMoon Nov 04 '14 at 06:17
  • 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! – elpita Nov 04 '14 at 13:53

5 Answers5

38

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 */
}}
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)
Josh
  • 529
  • 6
  • 21
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)
}
  • 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 wrong – Tad Jan 04 '22 at 00:40
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)
Ken Garber
  • 51
  • 7
Mr. Developer
  • 3,295
  • 7
  • 43
  • 110
0

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)
Bill peek
  • 90
  • 8