我从来没有遇到过 mouseOver 和 mouseOut 的问题。
但你可以使用 hitTestPoint:
function detectMouseOver(d:DisplayObject):Boolean
{
var mousePoint:Point = d.localToGlobal(new Point(d.mouseX,d.mouseY));
return d.hitTestPoint(mousePoint.x,mousePoint.y,true);
}
如果您确定该属性可用并从您调用的位置设置,您还可以使用 stage.mouseX 和 stage.mouseY (而不是 localToGlobal)。
我还没有测试过代码,但我认为它应该可以工作。
(edit)
但是,如果您想绝对确定鼠标是否经过了某个对象 - 即使您走得太快以至于完全跳过了该对象,您也必须检查两个帧的鼠标点之间的点。
例如,这样就可以了:
d.addEventListener(Event.ENTER_FRAME, checkMouseOver);
var lastPoint:Point;
const MAX_DIST:Number = 10;
function checkMouseOver(e:Event):void
{
var isOver:Boolean = false;
var d:DisplayObject = e.currentTarget as DisplayObject;
var thisPoint:Point = d.localToGlobal(new Point(d.mouseX,d.mouseY))
if (lastPoint)
while (Point.distance(thisPoint,lastPoint) > MAX_DIST)
{
var diff:Point = thisPoint.subtract(lastPoint);
diff.normalize(MAX_DIST);
lastPoint = lastPoint.add(diff);
if (d.hitTestPoint(lastPoint.x,lastPoint.y,true))
{
isOver = true;
break;
}
}
if (d.hitTestPoint(thisPoint.x,thisPoint.y,true))
isOver = true;
lastPoint = thisPoint;
//do whatever you want with isOver here
}
您可以记住最后一个状态是否结束,并在 isOver != wasOver 时分派自定义事件。如果您在 while 循环内执行此操作,您将获得高度准确的鼠标悬停检测。
但我敢打赌,带有 shapeFlag = true 的 hitTestPoint 会占用相当多的 CPU 资源,尤其是在一帧中大量使用的情况下。因此,在这种情况下,您可能希望将 MAX_DIST 设置得尽可能高。