Key*_*awn 4 ios sprite-kit swift
嗨,我是swift和SpriteKit的新手,但我目前正在开发一款游戏.在我的游戏中,我想要一个点对象圈.我查了一切,但我似乎无法弄明白.
我想要的是一个圆点,每个点都是一个单独的节点,我可以使用它来为每个单独的节点添加动画.
有没有办法在这样的圆形图案中绘制多个形状?
我希望它看起来像这样.
我将不胜感激任何帮助.我将继续研究并尝试解决问题.谢谢!
Ale*_*ano 13
您可以使用几行代码轻松绘制一个圆点:
SpriteKit:
PS更改模式参数以获得您想要做的事情.
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)
}
Run Code Online (Sandbox Code Playgroud)
结果:
或者使用传统代码:
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)
}
Run Code Online (Sandbox Code Playgroud)
结果:
最后,如果你想绘制每一个形状来制作动画,你也可以使用像这样的数学算法.
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)
}
}
Run Code Online (Sandbox Code Playgroud)
结果:
| 归档时间: |
|
| 查看次数: |
4068 次 |
| 最近记录: |