在圆形 CGPath 上添加精灵 Swift

Add sprites over a circular CGPath Swift

如何在圆形路径的坐标上重复生成精灵?

所以我有一个圆形路径,例如我希望精灵每 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 */
}
}

您的代码有很多语法错误。这就是为什么它不起作用。更正错误后,您使用的逻辑有效。

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

}