1

I'm trying to use the following SKNode method:

public func convertPoint(point: CGPoint, fromNode node: SKNode) -> CGPoint

The method works as I'd expect in some situations, but not in others. For example:

class ViewController: UIViewController {
    @IBOutlet var sceneView: SKView!
    private var scene = SKScene()
    private var root = SKNode()
    private var node1 = SKSpriteNode(color: UIColor.redColor(), size: CGSizeMake(10, 10))
    private var node2 = SKSpriteNode(color: UIColor.blueColor(), size: CGSizeMake(10, 10))

    override func viewDidLoad() {
        super.viewDidLoad()
        node1.anchorPoint = CGPointZero
        node2.anchorPoint = CGPointZero
        root.addChild(node1)
        root.addChild(node2)
        node1.position = CGPointMake(10, 10)
        node2.position = CGPointMake(20, 20)
        scene.scaleMode = .ResizeFill
        scene.addChild(root)
        sceneView.presentScene(scene, transition: SKTransition())
        print("position1 \(root.convertPoint(node2.position, toNode: node1))")
        print("position2 \(root.convertPoint(node1.position, toNode: node2))")
        print("position3 \(root.convertPoint(CGPointMake(0, 0), fromNode: node1))")
        print("position4 \(root.convertPoint(CGPointMake(0, 0), fromNode: node2))")
        print("position5 \(node1.convertPoint(CGPointMake(0, 0), fromNode: node2))")
        print("position6 \(node2.convertPoint(CGPointMake(0, 0), fromNode: node1))")
    }
}

enter image description here

This prints the following output:

position1 (10.0, 10.0)
position2 (-10.0, -10.0)
position3 (10.0, 10.0)
position4 (20.0, 20.0)
position5 (0.0, 0.0)
position6 (0.0, 0.0)

The first four outputs are as I'd expect, but I'm struggling to see how the last two are correct. Can anyone explain this?

(Xcode 7.1.1, iOS 9)

antsyawn
  • 971
  • 10
  • 17
  • Its probably because the second argument states "Another node in the same node tree as this node.", and SpriteKit treats sibling nodes as being in separate "sub"-trees. So if A and B are siblings, A is not in the parent hierarchy of B and vice versa. – Benzi Dec 03 '15 at 06:31

0 Answers0