如何使用SpriteKit在Swift中绘制一个圆形?

29

我刚开始使用Swift进行iOS开发,但我不知道如何画一个圆。我只是想画一个圆,将其设置为变量并在屏幕上显示,以便稍后将其用作主要玩家。有人能告诉我如何做到这一点或为此提供代码吗?

我在网上找到了这段代码:

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)

但是当我在Xcode上运行该应用程序时,在游戏场景中什么也没有显示出来。


1
你在什么时候调用了代码? - ZeMoon
2
如果您在 iPhone 模拟器上运行代码,则无法看到圆圈,因为其位置超出了视图的边界。尝试使用 circle.position = CGPointMake(100,100) 或其他值。 - ZeMoon
谢谢ZeMoon,那是我没有注意到的愚蠢错误。我将其设置为100,100,现在我可以看到它了。再次感谢。还要感谢0x141E! - elpita
5个回答

38

屏幕截图

如果您只想在某个位置绘制一个简单的圆形,可以使用以下代码:

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)
}
下面的代码会在用户触摸的地方绘制一个圆。 你可以用下面的代码替换默认的iOS SpriteKit项目中的"GameScene.swift"代码。

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 */
}}

2
理想情况下,您不希望使用CGPointMake。在Swift中,直接使用CGPoint更为推荐。 - William T Froggard

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)

3

在现代Swift(Xcode 13.2.1和Swift 5.1)中,创建项目时尝试使用苹果默认的游戏模板。然后删除actions文件,并将gameScene.swift中GameScene类的内容替换为以下内容:

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)
}

这是糟糕的建议,让某人创建一个全新的项目只是为了放置那一小段代码。如果我们不是在制作游戏而是在使用SpriteKit做其他事情呢?这就是错误的。 - Tadreik

1
我检查了你的代码,它是可以工作的,但可能发生的情况是球消失了,因为你将dynamic设置为true。关闭它然后再试一次,球就不会掉出场景了。
 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)

1
尝试这个。
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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接