ios - Spritekit - 如何计算两个节点之间的距离? [英] ios - Spritekit - How to calculate the distance between two nodes?

查看:118
本文介绍了ios - Spritekit - 如何计算两个节点之间的距离?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

屏幕上有两个sknodes。计算距离的最佳方法是什么('作为乌鸦飞行的距离类型,我不需要矢量等)?

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上没有太多线程)

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 - 如何计算两个节点之间的距离?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆