-1

Im looking to create a swift function to return the degrees (Float) of the users touch on an image (SKSpritenode). In touchesBegan, I know how to detect the x & y positions of my image. The idea is to create a function that takes in these positions and returns the degrees.

Amended - The following code now works:

    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)") 

        }

    }
}
pete
  • 2,980
  • 4
  • 22
  • 34
  • 1
    well, what have you tried? You need to calculate the distance of the (x,y) in regards to the center, if not done already and then use the correct sin/cos/tan arithmetics. – luk2302 Dec 12 '15 at 16:52

1 Answers1

3

You need to compare the user's touch position to an origin point, which might be the centre of your sprite node for example. Here's some code to get you started:

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)

That last value – degrees – is the one you want if you want to show users information. If you want to do more calculations, you should probably stick with radians.

TwoStraws
  • 12,862
  • 3
  • 57
  • 71
  • Hi TwoStraws - thanks for your help but I think I need to read up more on trigonometry. Anyway, I tried your suggestion (see above ). When running the app, if I touch somewhere near (6 o'clock) on my image, the degrees = 10.2188500342174. I want it to be > or < or == 180 ?? – pete Dec 12 '15 at 18:55
  • @pete: The code is definitely correct, so it's more likely the touch and/or origin points are wrong. – TwoStraws Dec 12 '15 at 19:14
  • Is my amended code correct ? Ive tried all different combinations but my print out is always incorrect. Ive tried on device and simulator. – pete Dec 13 '15 at 09:34
  • It's possible the touch position is incorrect. Are you translating it somehow? Keep in mind that the location of a touch in node depends on which node you compare it against. – TwoStraws Dec 13 '15 at 09:37
  • Ive amended my question to show how I've set up touches to detect the touch within my node. This works, but again the print out is wrong. I really appreciate your help but can you see where I'm going wrong ? – pete Dec 13 '15 at 10:41
  • @pete, print the value of the coordinates and the result. Also note that `let origin = CGPoint(x: myNode.frame.origin.x, y: myNode.frame.origin.y)` can be written as `let origin = myNode.frame.origin` and `let touch = CGPoint(x: location.x, y: location.y)` as `let touch = location` – jcaron Dec 13 '15 at 10:53
  • You need to compare locations that are in the same reference frame. If `myNode` is not a child of `self`, then it won't work. Use `let location = touch.locationInNode(myNode.parent)` (or conversely, convert the node's location to the frame of your scene). – jcaron Dec 13 '15 at 10:56
  • Also, you may need to take into account the anchor point of `myNode` – jcaron Dec 13 '15 at 10:56
  • @jcaron, thanks for help. As above, I have corrected my code with your suggestions but it still gives out wrong numbers (see example touch at 6 o'clock). Can you see anything wrong ? – pete Dec 14 '15 at 10:21
  • @pete: The code I posted is correct, but given that it's still not working it would suggest something else is at play that is breaking things. Could you post your project somewhere? – TwoStraws Dec 14 '15 at 10:23
  • @TwoStraws - would a DropBox link be ok ? – pete Dec 14 '15 at 10:40
  • So: my code was correct, but your method of pulling out the origin of your node was incorrect. Please replace `myNode.frame.origin` with `myNode.position` and the problem goes away. – TwoStraws Dec 14 '15 at 10:58
  • @TwoStraws - Yep that did it. Thank you so much for sticking with me on this problem. Really appreciate it. – pete Dec 14 '15 at 11:18