ios - Spritekit - How to calculate the distance between two nodes?

后端 未结 5 968
有刺的猬
有刺的猬 2020-12-14 21:19

I have two sknodes on the screen. What is the best way to calculate the distance (\'as the crow flies\' type of distance, I don\'t need a vector etc)?

I\'ve had a go

5条回答
  •  情书的邮戳
    2020-12-14 21:56

    Here's a function that will do it for you. This is from an Apple's Adventure example code:

    CGFloat SDistanceBetweenPoints(CGPoint first, CGPoint second) {
        return hypotf(second.x - first.x, second.y - first.y);
    }
    

    To call this function from your code:

    CGFloat distance = SDistanceBetweenPoints(nodeA.position, nodeB.position);
    

提交回复
热议问题