如何使用鼠标坐标判断鼠标光标是否位于组件上? [英] How to tell if the mouse cursor is over a component using the mouse coordinates?
问题描述
我正在尝试借助鼠标事件来确定我是否在特定组件上.
I'm trying to determine with the help of a mouse event if I'm over a specific component.
所以现在我有两个组件,比如说 buttonA 和 buttonB.ButtonA 上有一个监听器,它已经在监听鼠标离开事件.ButtonB 与 ButtonA 的边缘齐平.
So right now I have two components, let's say buttonA and buttonB. ButtonA has a listener on it already listening for the mouse out event. ButtonB is flush against the edge of ButtonA.
我需要找出它的代码.以下是更多详细信息:
I need to find out the code for it. Here is more details:
protected function _mouseOutHandler(event:MouseEvent):void
{
if (data.subCategories.length>0)
{
if (MOUSE IS NOT OVER BUTTONB) {
}
if (MOUSE IS OVER DROPDOWNB) {
}
}
}
推荐答案
我最终做的(从上述回复中获得灵感)是设置一个超时时间,让鼠标有时间移动,然后在该超时处理程序中使用 hitTestPoint 来检查鼠标是否在任一组件上.代码如下:
What I ended up doing (with inspiration from the above replies) is setting a timeout to give the mouse time to travel and then in that timeout handler using hitTestPoint to check if the mouse is over either components. Here is the code:
private var timeout:uint;
/**
* On mouse out of item renderer set timeout
* */
protected function _mouseOutHandler(event:MouseEvent):void {
if (data.subCategories.length>0) {
timeout = setTimeout(checkToClose, 150);
}
}
/**
* Handles when mouse moves out of drop down
* */
protected function _menuMouseOutHandler(event:MouseEvent):void {
checkToClose();
}
/**
* Check if mouse is out of drop down and category renderer
* */
public function checkToClose():void {
var point:Point;
clearTimeout (timeout);
// check if drop down is open
if (menu.dropDown) {
point = localToGlobal(new Point(mouseX, mouseY));
menu.dropDown.addEventListener(MouseEvent.MOUSE_OUT, _menuMouseOutHandler, false, 0, true);
// check if we are over drop down or category renderer
// if not close dropdown
if (!menu.dropDown.hitTestPoint(point.x, point.y)
&& !hitTestPoint(point.x, point.y)) {
menu.dropDown.removeEventListener(MouseEvent.MOUSE_OUT, _menuMouseOutHandler);
menu.closeDropDown(false);
}
}
}
这篇关于如何使用鼠标坐标判断鼠标光标是否位于组件上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!