【发布时间】:2015-03-25 00:30:36
【问题描述】:
如何在圆形路径的坐标上重复生成精灵?
所以我有一个圆形路径,例如,我希望精灵每 30º 出现一次(所以在这种情况下,我最终会拥有相同的精灵 12 次)。问题是我做什么都没关系,我不能让它这样做。最近我发现一篇文章展示了如何制作时钟,我使用了该示例代码,但我仍然卡住了,我不知道用什么替换 drawRect 或者我是否有任何其他错误。 我将不胜感激。
import SpriteKit
import UIKit
class GameScene: SKScene {
override func didMoveToView(view: SKView) {
/* Setup your scene here */
func degree2radian(a:CGFloat)->CGFloat {
let b = CGFloat(M_PI) * a/180
return b
}
func circleCircumferencePoints(sides:Int,x:CGFloat,y:CGFloat,radius:CGFloat,adjustment:CGFloat=0)->[CGPoint] {
let angle = degree2radian(360/CGFloat(sides))
let cx = x
let cy = y
let r = radius
var i = sides
var points = [CGPoint]()
while points.count <= sides {
let xpo = cx - r * cos(angle * CGFloat(i)+degree2radian(adjustment))
let ypo = cy - r * sin(angle * CGFloat(i)+degree2radian(adjustment))
points.append(CGPoint(x: xpo, y: ypo))
i--;
}
return points
}
func pointsLocation(#x:CGFloat, #y:CGFloat, #radius:CGFloat, #sides:Int) {
let points = circleCircumferencePoints(sides,x,y,radius)
let path = CGPathCreateMutable()
for p in enumerate(points) {
CGPathMoveToPoint(path, nil, p.element.x, p.element.y)
let blueDot = SKSpriteNode(imageNamed: "arrow.png")
blueDot.position.x = p.element.x
blueDot.position.y = p.element.y
addChild(blueDot)
CGPathCloseSubpath(path)
class View: UIView {
override func drawRect(rect: CGRect)
let rad = CGRectGetWidth(rect)/3.5
pointsLocation(x: CGRectGetMidX(rect), y: CGRectGetMidY(rect), radius: rad, sides: 8)
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
}
【问题讨论】:
-
我试过你的代码,它可以工作。
-
你改变了什么吗?它显示我的9个错误。
标签: swift sprite-kit geometry cgpath