我有一个.swf文件,其中包含一个圆圈,我从URL加载该圆圈,制作成movieClip并将其添加到舞台上。
我希望能够检测用户何时将光标放在圆圈上。我希望它在某种意义上是准确的,它不是简单地使用圆的边界框,而是使用实际的圆本身来检测碰撞。这是我当前代码的片段:
//
var myCircle:MyCircle = new MyCircle(swf);
myCircle.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
myContainer.addChild(myCircle);
//
private function onMouseMove(event:MouseEvent):void
{
var glowFilter:GlowFilter = new GlowFilter();
glowFilter.color = 0xffffff;
if (event.currentTarget is MyCircle) {
var object:MyCircle = event.currentTarget as MyCircle;
if(object.hitTestPoint(event.stageX, event.stageY, true))
event.currentTarget.filters = [glowFilter];
else
event.currentTarget.filters = [];
}
}
我被引导相信hitTestPoint()
方法的shapeFlag
参数会做到这一点,但将其设置为 true 或 false 都没有区别。知道为什么会这样吗?
我之前写了一篇关于这个的非常短的博客文章,所以我把它放在将来的时候......
似乎您正在查看 stageX,其中 AFAIK 是对象的全局 x 坐标。
http://messer1024.blogspot.se/2012/06/proper-hittest-in-as3.html
短文的短版本:
在 AS3 中处理命中测试的多次丑陋尝试之后,我终于设法解决了我正在寻找的问题。我忘了考虑的是使用全局鼠标坐标。
public function hitTestMouse(hitarea:DisplayObject):Boolean {
return hitarea.hitTestPoint(hitarea.stage.mouseX, hitarea.stage.mouseY, true);
}
最后一个参数 (shapeFlag) 将决定是否检查命中测试鼠标悬停在上面的命中区域内的任何实际形状。
所以为了让你的东西工作,你可能应该让它更容易一点,添加我的函数,然后在你想测试它的时候运行"hitTestMouse(myCircle)"。