鼠标与组件之间的距离

发布于 2024-10-27 03:47:15 字数 102 浏览 4 评论 0原文

不确定是否存在,但值得问大家:是否有内置方法来计算当前鼠标位置与给定组件之间的距离?如果没有,是否有一种简单的方法来构建这样的函数,该函数适用于具有通用形状的组件?

谢谢你!

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌路黄昏 2024-11-03 03:47:15

好吧,假设您想要从鼠标到左上角的距离(在本例中是 Flex 的默认距离),只需使用毕达哥拉斯定理:

var d:int = Math.sqrt(Math.pow(theComponent.mouseX, 2) + Math.pow(theComponent.mouseY, 2));

同样,这将是到“theComponent”左上角的距离。如果您希望它位于组件的中心,请执行以下操作:

var d:int = Math.sqrt(Math.pow(theComponent.mouseX - theComponent.width/2, 2) + Math.pow(theComponent.mouseY - theComponent.height/2, 2));

每个 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:

var d:int = Math.sqrt(Math.pow(theComponent.mouseX, 2) + Math.pow(theComponent.mouseY, 2));

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:

var d:int = Math.sqrt(Math.pow(theComponent.mouseX - theComponent.width/2, 2) + Math.pow(theComponent.mouseY - theComponent.height/2, 2));

Every DisplayObject has this 'mouseX/Y' property which is always relative to the top left corner.

少跟Wǒ拽 2024-11-03 03:47:15

我想我已经为你准备了一个解决方案,我不相信有任何内置的东西可以直接为你做到这一点,尽管可能有比这更好的方法......但基本上我能想到的任何解决方案使用相同的概念,所以这里是:

    private var lastClickedComponent:DisplayObject;
    private var lastClickedGlobalPos:Point;

    protected function application1_clickHandler(event:MouseEvent):void
    {
        // TODO Auto-generated method stub
        lastClickedComponent = event.target as DisplayObject;
        if(lastClickedComponent)
            lastClickedGlobalPos = lastClickedComponent.parent.localToGlobal(new Point(lastClickedComponent.x,lastClickedComponent.y));
    }

    private function distanceToLastClicked():void
    {
        if(lastClickedComponent)
        {
            distanceLabel.text = Point.distance(lastClickedGlobalPos,new Point(mouseX,mouseY)).toString();
        }
    }


    protected function application1_mouseMoveHandler(event:MouseEvent):void
    {
        distanceToLastClicked();
    }

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:

    private var lastClickedComponent:DisplayObject;
    private var lastClickedGlobalPos:Point;

    protected function application1_clickHandler(event:MouseEvent):void
    {
        // TODO Auto-generated method stub
        lastClickedComponent = event.target as DisplayObject;
        if(lastClickedComponent)
            lastClickedGlobalPos = lastClickedComponent.parent.localToGlobal(new Point(lastClickedComponent.x,lastClickedComponent.y));
    }

    private function distanceToLastClicked():void
    {
        if(lastClickedComponent)
        {
            distanceLabel.text = Point.distance(lastClickedGlobalPos,new Point(mouseX,mouseY)).toString();
        }
    }


    protected function application1_mouseMoveHandler(event:MouseEvent):void
    {
        distanceToLastClicked();
    }

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文