本文介绍了ios - Spritekit - 如何计算两个节点之间的距离?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在屏幕上有两个 sknode.计算距离的最佳方法是什么('as the crow flies' 类型的距离,我不需要矢量等)?
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)?
我用谷歌搜索并在这里搜索,但找不到涵盖此内容的内容(stackoverflow 上没有太多关于 sprite kit 的线程)
I've had a google and search on here and can't find something that covers this (there aren't too many threads on stackoverflow about sprite kit)
推荐答案
这里有一个函数可以帮你解决.这是来自 Apple 的 Adventure 示例代码:
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);
这篇关于ios - Spritekit - 如何计算两个节点之间的距离?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!