How to convert the coordinates of one SpriteNode to another that is not a child or descendant

Marco Boerner

In my SpriteKit Scene I got the following setup:

Several nodes with the name sourceNode are direct children of the scene. A node called targetNode is a child of another node called sectionNode, sectionNode is a child of the scene as well.

self --> sourceNode

self --> sectionNode --> targetNode

When using the touchesMoved method to move the sourceNode I set the position of the sourceNode to the current location of the touch. Once the touch location is above the targetNode I want to snap the sourceNode to the targetNode by matching their positions.

My issue:

I do not seem to be able to properly convert in between the two coordinate spaces. I tried different variations of convert(_:from:) and convert(_:to:), but none of it works. I know convert expects both nodes to be in the same node tree, but as both of them are descendants of the scene, aren't they considered part of the same node tree?

(If both sourceNode and targetNode are in the same coordinate system as siblings, the code works by just assigning the position of the targetNode to the position of the sourceNode)

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let touch = touches.first {
        let touchLocation = touch.location(in: self)
        let touchedNodes = nodes(at: touchLocation)

        guard let sourceNode = touchedNodes.filter({$0.name == "sourceNode"}).first else { return }

        if let targetNode = touchedNodes.filter({$0.name == "targetNode"}).first {
            
            // sourceNode.position = targetNode.position    <-- works if both have the same parents   

            sourceNode.position = convert(targetNode.position, to: sourceNode)
        } else {
            sourceNode.position = touchLocation
        }
    }
}
JohnL

To move/snap the sourceNode to the coordinates of the targetNode, you could do the following:

sourceNode.move(toParent: sectionNode)
sourceNode.position = targetNode.position
sourceNode.move(toParent: scene)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to move a child object towards another coordinates?

How can I tell if one commit is a descendant of another commit?

How to select element by specific child/descendant element

How to paint specific coordinates of one JPanel onto another JPanel

how to know events of one child component in another child Component

XSLT How to append one child element if another child element is repeating

How to convert one struct to another in Go when one includes another?

How to convert coordinates to a shapefile

how to convert coordinates

How do I rotate a SpriteNode with a one finger “touch and drag”

How to convert one partial function to another?

How to convert one enum to another enum in java?

How to convert one type of object array into another?

How to convert json of one format to another format

How to convert a string from one codepage to another?

How to convert string from one encoding to another

How to convert codepoint of one charset to another in Java?

How to convert "normal" coordinates to OSM coordinates in OpenLayers

How to convert spherical coordinates to equirectangular projection coordinates?

How to convert workspace coordinates to screen coordinates?

How to Convert World Coordinates to Camera Image Coordinates?

How to convert svg element coordinates to screen coordinates?

How to convert cartesian coordinates to polar coordinates in JS?

Python:How to convert pymouse coordinates to turtle coordinates

how to convert normalized coordinates to pixel coordinates?

How do make multiple plots in r, one for each vector that needs to create coordinates with another one?

convert 2D coordinates to another 2D coordinates

How to return an XML element value from a Descendant when passing in another element value from that Descendant

How to pass value from one child component to another in VueJS?