如何在圆形路径的坐标上重复生成精灵?
所以我有一个圆形路径,我希望精灵每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 */
}
}
答案 0 :(得分:2)
您的代码有很多语法错误。这就是为什么它不起作用。纠正错误后,您使用的逻辑可以正常工作。
class GameScene: SKScene,SKPhysicsContactDelegate {
override func didMoveToView(view: SKView) {
pointsLocation(x: 100, y: 100, radius: 50, sides: 12)
}
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: x,y: y,radius: radius)
let path = CGPathCreateMutable()
for p in enumerate(points) {
let blueDot = SKSpriteNode(imageNamed: "arrow.png")
blueDot.position.x = p.element.x
blueDot.position.y = p.element.y
addChild(blueDot)
}
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
}