如何在swift中绘制一个圆点作为单个节点(SpriteKit)

时间:2016-05-28 16:23:26

标签: ios swift sprite-kit

嗨,我是swift和SpriteKit的新手,但我目前正在开发游戏。在我的游戏中,我想要一个点对象圈。我查了一切,但我似乎无法弄明白。

我想要的是一个圆点,每个点都是一个单独的节点,我可以用它来为每个节点添加动画。

有没有办法以这样的圆形图案绘制多个形状?

我希望它看起来像这样。

Parametric Presentation of a Circle

我将不胜感激任何帮助。我将继续研究并尝试解决问题。谢谢!

1 个答案:

答案 0 :(得分:13)

您可以使用几行代码轻松绘制一个圆点:

<强> SpriteKit:

P.S。更改模式参数以获得您想要做的事情。

func circleOfDots() {
        let circlePath = UIBezierPath(arcCenter: CGPoint(x: 200,y: 200), radius: CGFloat(100), startAngle: CGFloat(0), endAngle:CGFloat(M_PI * 2), clockwise: true)
        let shapeNode = SKShapeNode()
        shapeNode.path = circlePath.CGPath
        shapeNode.position =  CGPoint(x:CGRectGetMidX(self.frame)-200, y:CGRectGetMidY(self.frame)-200)
        //change the fill color
        shapeNode.fillColor = UIColor.clearColor()
        //you can change the stroke color
        shapeNode.strokeColor = UIColor.redColor()
        //you can change the line width
        shapeNode.lineWidth = 6.0
        let one : CGFloat = 1
        let two : CGFloat = 10
        let pattern = [one,two]
        let dashed = CGPathCreateCopyByDashingPath(circlePath.CGPath,nil,0,pattern,2)
        shapeNode.path = dashed
        shapeNode.lineCap = CGLineCap.Round
        self.addChild(shapeNode)
    }

结果:

enter image description here

或使用传统代码:

<强>的UIKit

func circleOfDots() {
        let circlePath = UIBezierPath(arcCenter: CGPoint(x: 200,y: 200), radius: CGFloat(100), startAngle: CGFloat(0), endAngle:CGFloat(M_PI * 2), clockwise: true)
        let shapeLayer = CAShapeLayer()
        shapeLayer.path = circlePath.CGPath
        shapeLayer.position = CGPointMake(CGRectGetMidX(self.view.bounds)-200 ,CGRectGetMidY(self.view.bounds)-200 )
        //change the fill color
        shapeLayer.fillColor = UIColor.clearColor().CGColor
        //you can change the stroke color
        shapeLayer.strokeColor = UIColor.redColor().CGColor
        //you can change the line width
        shapeLayer.lineWidth = 6.0
        let one : NSNumber = 1
        let two : NSNumber = 20
        shapeLayer.lineDashPattern = [one,two]
        shapeLayer.lineCap = kCALineCapRound
        self.view.layer.addSublayer(shapeLayer)
    }

结果:

enter image description here

最后,如果你想绘制每一个形状来制作动画,你也可以使用像这样的数学方法。

<强> SpriteKit

func circleOfDots() {
        let radius: CGFloat = 100.0
        let numberOfCircle = 30
        for i in 0...numberOfCircle {

            let circle = SKShapeNode(circleOfRadius: 2 )
            circle.strokeColor = SKColor.clearColor()
            circle.glowWidth = 1.0
            circle.fillColor = SKColor.orangeColor()
            // You can get every single circle by name:
            circle.name = String(format:"circle%d",i)
            let angle = 2 * M_PI / Double(numberOfCircle) * Double(i)

            let circleX = radius * cos(CGFloat(angle))
            let circleY = radius * sin(CGFloat(angle))

            circle.position = CGPoint(x:circleX + frame.midX, y:circleY + frame.midY)
            addChild(circle)
        }
}

结果:

enter image description here