Position of iOS spriteKit child nodes relative to view coordinates

I have a child node added to another node. I want to get the position of the child nodes relative to the coordinates of the views, not the coordinates of the parent nodes

+4
source share
2 answers

Get the child of the node positionwith respect to its parent, then use convertPoint:fromNode:either convertPoint:toNode:the conversion method from the parent coordinate system of the node to the scene coordinate system. (Remember, it SKSceneinherits from SKNodeand is the root of the node hierarchy, so you can use it with any of these methods.) If you need to work in the coordinate space of the UIKit view, use a scene convertPointToView:.

+16
source

If by “viewing coordinates” you understand the coordinates of the scene, then check out this quick solution, which I use:

extension SKNode {
    var positionInScene:CGPoint? {
        if let scene = scene, let parent = parent {
            return parent.convert(position, to:scene)
        } else {
            return nil
        }
    }
}

Then you can get the scene position in the same way as a regular position. Here is an example:

let positionInParent = childNode.position
let positionInScene  = childNode.positionInScene? //optional return type

, positionInScene , SKNode , node . . , node , .

0

Source: https://habr.com/ru/post/1527589/


All Articles