如何将圆周上的精灵旋转到中心?

时间:2016-07-22 13:25:41

标签: swift macos sprite-kit geometry

我在圆周上有 n 精灵:

let radius = 100
let n = 10

let parentNode = SKSpriteNode(color: NSColor.yellowColor(), size: CGSizeMake(CGFloat(radius), CGFloat(radius)))
parentNode.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
parentNode.zPosition = 1.5

for sprite in 0...n {
    let aSprite = SKSpriteNode(imageNamed: "image.png")
    aSprite.setScale(0.15)
    aSprite.name = "sprite"+String(sprite)

    let theta = Float(Double(sprite) * ((2 * M_PI)/n))
    let x = radius * Double(cosf(theta))
    let y = radius * Double(sinf(theta))

    aSprite.position = CGPointMake(CGFloat(x), CGFloat(y))

    parentNode.addChild(aSprite)
}

我想将每个精灵旋转到圆圈的中心:

CGPoint(CGRectGetMidX(parentNode.frame),CGRectGetMidY(parentNode.frame))

我尝试使用atan2来完成此操作,但它无法正常工作:

aSprite.zRotation = atan2(CGRectGetMidY(parentNode.frame)-aSprite.position.y, CGRectGetMidX(parentNode.frame) - aSprite.position.x)

0 个答案:

没有答案