如何围绕屏幕中心旋转SpriteNode?

时间:2017-06-18 22:16:21

标签: ios sprite-kit

我正在更新我当前的应用程序,我想知道是否有办法在中心周围旋转精灵节点?例如,地球围绕太阳旋转。我也在使用sks文件。

这就是我所拥有的:

import SpriteKit
import GameplayKit

class StartScene: SKScene  {

var singleTap = SKSpriteNode()

var path = UIBezierPath()

var gameStarted = Bool()

override func didMove(to view: SKView) {

    singleTap = self.childNode(withName: "single") as! SKSpriteNode
    singleTap.anchorPoint = CGPoint(x: 0.5, y: 0.5)

    moveCounterClockwise()


}


override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {


}



 func moveCounterClockwise() {




    let dx = singleTap.position.x - self.frame.midX
    let dy = singleTap.position.y - self.frame.midY

    let rad = atan2(dy, dx)


    path = UIBezierPath(arcCenter: CGPoint(x: self.frame.midX, y: self.frame.midY), radius: 120, startAngle: rad, endAngle: rad + CGFloat(Double.pi * 4), clockwise: true)

    let follow = SKAction.follow(path.cgPath, asOffset: false, orientToPath: true, speed: 60)

    singleTap.run(SKAction.repeatForever(follow))

}


override func update(_ currentTime: TimeInterval) {
    // Called before each frame is rendered



   }
}

此代码使精灵围绕中心旋转,但它也将精灵旋转90度。

我也看了这个答案,但我无法理解如何写它:https://stackoverflow.com/a/19045698/3926691

编辑: 这两张图片是基于我想要完成的和正在发生的事情。看起来UIBezierPath正在引起某种不必要的Z-Rotation。

enter image description here enter image description here

欢迎任何帮助,如果需要,我可以尝试提供清晰度。

1 个答案:

答案 0 :(得分:1)

如果您不希望您的精灵在按照您为其定义的路径的同时进行旋转,请将orientToPath设置为false

更改:

let follow = SKAction.follow(path.cgPath, asOffset: false, orientToPath: true, speed: 60)

let follow = SKAction.follow(path.cgPath, asOffset: false, orientToPath: false, speed: 60)

(首先尝试let follow = SKAction.follow(path.cgPath, speed: 60)asOffsetorientToPath默认为false,我认为)