TimKrs
TimKrs

Reputation: 67

How to check if a node is near to another one?

I would like to test which nodes are near another particular node.

In the following example, the green field is used to check which fields are nearby. It should return the yellow, orange (horizontal to green and only one field removed) and blue (diagonal and only one field removed) field.

enter image description here

Each field is a node:

    let greenField = SKSpriteNode(imageNamed: "greenField")
    greenField.zPosition = 1

    let yellowField = SKSpriteNode(imageNamed: "yellowField")
    yellowField.zPosition = 1

    ...

Does anyone have any idea how to test this? I would be very grateful for any answer.

Upvotes: 0

Views: 124

Answers (1)

Thiago Nicolau
Thiago Nicolau

Reputation: 146

You can check the distance of each node.

CGFloat distance = hypotf(p1.x - p2.x, p1.y - p2.y);

Tip: Make sure the anchor point is in the center to make it easier.

Upvotes: 1

Related Questions