鼠标与组件之间的距离
不确定是否存在,但值得问大家:是否有内置方法来计算当前鼠标位置与给定组件之间的距离?如果没有,是否有一种简单的方法来构建这样的函数,该函数适用于具有通用形状的组件?
谢谢你!
Not sure this exist, but worth asking you all: is there a built in method to compute the distance between the current mouse position and a given component? If not, is there a simple way to build a function like this that works for components with generic shapes?
thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,假设您想要从鼠标到左上角的距离(在本例中是 Flex 的默认距离),只需使用毕达哥拉斯定理:
同样,这将是到“theComponent”左上角的距离。如果您希望它位于组件的中心,请执行以下操作:
每个 DisplayObject 都有此“mouseX/Y”属性,该属性始终相对于左上角。
Okay, say you want the distance from the mouse to the top left corner (the default for Flex in this case), just use Pythagoras' theorem:
Again, this would be the distance from the top left corner of 'theComponent'. If you want it to be from the center of the component, do this:
Every DisplayObject has this 'mouseX/Y' property which is always relative to the top left corner.
我想我已经为你准备了一个解决方案,我不相信有任何内置的东西可以直接为你做到这一点,尽管可能有比这更好的方法......但基本上我能想到的任何解决方案使用相同的概念,所以这里是:
distanceLabel 只是一个标签,处理程序只是在本示例的应用程序上设置,但基本上唯一重要的部分是为操作点而给出的距离函数以及用于转换 x/ 的 localToGlobal 调用DisplayObject 的 y 位置到绝对坐标,以便与鼠标位置进行比较(请注意,您可能需要在移动处理程序中使用 event.stageX、event.stageY,具体取决于您处理的对象,我不确定 mouseX、 mouseY 是全局坐标)。另外,正如评论中所指出的,这个只会考虑形状的左上角,不一定是最近的边缘,因为你可能需要做一些特定于形状的数学,除非有人有更新颖的方法。
Think I've got a solution put together for ya, I don't believe there's anything built in that'll do this for you outright though there may be a better way than this... but basically any solution I can think of basically uses the same concept so here it is:
distanceLabel is just a Label the handlers are just set on the application for this example but basically the only part that's important is the distance function given for operating on points and the localToGlobal call to convert the x/y position of the DisplayObject to absolute coordinates for comparison with the mouse position (to note you might need to use event.stageX, event.stageY in the move handler depending on what object you have handling that, I'm not sure that mouseX,mouseY are global coordinates). Also as noted in the comment this one is only going to take into account the top left corner of the shape not necessarily the closest edge for that you'll probably need to do some shape specific math unless someone has a more novel way.