【问题标题】:Add sprites over a circular CGPath Swift在圆形 CGPath Swift 上添加精灵
【发布时间】: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


【解决方案1】:

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

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

}

【讨论】:

  • 如果路径从不使用,为什么需要行“let path = CGPathCreateMutable()”?
  • 如果从未使用过,则可能不需要该变量。正如我在回答中指出的那样,我只是纠正了他的语法错误而不是他的逻辑。
猜你喜欢
  • 1970-01-01
  • 2023-03-21
  • 1970-01-01
  • 1970-01-01
  • 2013-06-07
  • 1970-01-01
  • 2016-04-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多