Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw a circle in swift using spriteKit?

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.

like image 218
elpita Avatar asked Nov 04 '14 04:11

elpita


People also ask

Can you use SpriteKit with SwiftUI?

Even though the default Game Xcode Template creates the project based on a UIKit application, you can create a SwiftUI app and put your SpriteKit game inside it without any hustle thanks to the SpriteView view!

What is a SpriteKit?

SpriteKit is a general-purpose framework for drawing shapes, particles, text, images, and video in two dimensions. It leverages Metal to achieve high-performance rendering, while offering a simple programming interface to make it easy to create games and other graphics-intensive apps.


3 Answers

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 */
}}
like image 103
Voltaire's Ghost Avatar answered Oct 18 '22 03:10

Voltaire's Ghost


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)
like image 21
Josh Avatar answered Oct 18 '22 05:10

Josh


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)
}
like image 2
code_crafter Avatar answered Oct 18 '22 03:10

code_crafter