通过用户触摸检测圆圈中的度数

Detect the degrees in a circle with users touch

我想创建一个 swift 函数来 return 用户触摸图像 (SKSpritenode) 的度数 (Float)。在 touchesBegan 中,我知道如何检测图像的 x 和 y 位置。这个想法是创建一个接受这些位置和 returns 度数的函数。

已修改 - 以下代码现在有效:

    class GameScene: SKScene {
override func didMoveToView(view: SKView) {
    /* Setup your scene here */

   self.anchorPoint = CGPointMake(0.5, 0.5)
   myNode.position = CGPointMake(0, -myNode.frame.height / 2)
   self.addChild(myNode)

}


override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
   /* Called when a touch begins */

    for touch in touches {
        let location = touch.locationInNode(self)


            if myNode.containsPoint(location) {
            print("tapped!")
            let origin = myNode.position
            let touch = touch.locationInNode(myNode.parent!)
            let diffX = touch.x - origin.x
            let diffY = touch.y - origin.y
            let radians = atan2(diffY, diffX)
            let degrees = radians * CGFloat(180 / M_PI)

            print("degrees = \(degrees)") 

        }

    }
}

您需要将用户的触摸位置与原点进行比较,例如原点可能是精灵节点的中心。这里有一些代码可以帮助您入门:

let origin = CGPoint(x: 0, y: 0)
let touch = CGPoint(x: 100, y: 100)

let diffX = touch.x - origin.x
let diffY = touch.y - origin.y
let radians = atan2(diffY, diffX)
let degrees = radians * CGFloat(180 / M_PI)

最后一个值 - degrees - 是您想要显示用户信息的值。如果你想做更多的计算,你应该坚持使用 radians.