user3504730
user3504730

Reputation:

Converting Between Coordinate Planes

I'm currently working with multiple nodes acting as layers and I really need a way to convert the coordinates between them since I have multiple layers with varying children, it's a complicated scene so I don't want to lay it out in text. Any links would be much appreciated as well.

Upvotes: 0

Views: 85

Answers (2)

K3vN McAfeee
K3vN McAfeee

Reputation: 11

If you are having this much trouble with your layers that you are requiring converting points, you may want to just rethink your strategy on how you're laying out the game scene.

Upvotes: 1

Billy Jean
Billy Jean

Reputation: 193

Taken straight from Apple Docs

When working with the node tree, sometimes you need to convert a position from one coordinate space to another. For example, when specifying joints in the physics system, the joint positions are specified in scene coordinates. So, if you have those points in a local coordinate system, you need to convert them to the scene’s coordinate space.

The following method shows how to convert a node’s position into the scene coordinate system. The scene is asked to perform the conversion. Remember that a node’s position is specified in its parent’s coordinate system, so the code passes node.parent as the node to convert from. You could perform the same conversion in reverse by calling the convertPoint:toNode: method

CGPoint positionInScene = [node.scene convertPoint:node.position fromNode:node.parent];

One situation where you need to perform coordinate conversions is when you perform event handling. Mouse and touch events need to be converted from window coordinates to view coordinates, and from there into the scene. To simplify the code you need to write, Sprite Kit adds a few convenience methods:

In iOS, use the locationInNode: and previousLocationInNode: on UITouch objects to convert a touch location into a node’s coordinate system.

In OS X, use the locationInNode: method on NSEvent objects to convert a mouse event into a node’s coordinate system.

Upvotes: 1

Related Questions