AS3:命中测试任何对象

发布于 2024-10-30 13:10:29 字数 110 浏览 7 评论 0原文

我正在开发一个以图像作为光标的应用程序。现在我想随时知道光标悬停在哪个对象上。有点像 HitTestObject(*),然后我可以看到 * 代表什么对象。有谁知道我如何才能做到这一点? (并且不能使用鼠标)

I am working on an application where an image serves as cursor. Now i would like to know at any time over which object the cursor is hovering. Sort of like a HitTestObject(*) where i can then see what object the * represents. Does anyone have any idea how i could accomplish this? (and using the mouse is not an option)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

微凉 2024-11-06 13:10:29

将要监视“悬停”的元素放在单独的数组中,
然后向附加到鼠标的对象添加一个 onEnterFrame 侦听器,该侦听器循环访问数组并对每个对象执行 hitTests。

var hitTestClips:Array;
// populate hitTestClips with the items you want to hitTest

这将出现在鼠标附加对象的 onEnterFrame 处理程序中:

for(var item:MovieClip in hitTestClips)
{
  if(item.hitTest(this.x, this.y, true))
  {
    trace('now hovering above ' + item);
  }
}

Put the elements you want to monitor for 'hovering' in a separate array,
then add an onEnterFrame listener to the object attached to your mouse that iterates through the array and performs hitTests with each of the objects.

var hitTestClips:Array;
// populate hitTestClips with the items you want to hitTest

and this goes in the onEnterFrame handler for your mouse-attached object:

for(var item:MovieClip in hitTestClips)
{
  if(item.hitTest(this.x, this.y, true))
  {
    trace('now hovering above ' + item);
  }
}
陌若浮生 2024-11-06 13:10:29

我已经解决了这个问题:)由于光标位于与其他精灵不同的精灵中,我必须这样做,因为我无法将对象传递到数组中。

        //First we will create a point that contains the x and y of this cursor.
        var _position:Point = new Point(x + (width/2), y + (height/2));

        //Secondly, we will get an array of elements that are under this point.
        var _objects:Array = parentApplication.getObjectsUnderPoint(_position);

        //If the length of the objectsList is longer than or equal to 2, we may assume that
        //there is an object
        if(_objects.length >= 2)
        {
            //Set the currentObject variable to the object the cursor is hovering over.
            //The minus two is simple. The cursor is always the last object under that point,
            //so we need the object before that.
            _currentObject = _objects[_objects.length - 2];

            //dispatch the event in the object.
            dispatchCursorEventToObject(EyeEvent.CURSOROVER);
        }

I have solved the problem already :) since the cursor was in a different sprite than the others, i had to do it this way, because i couldn't pass the objects to hover into an array.

        //First we will create a point that contains the x and y of this cursor.
        var _position:Point = new Point(x + (width/2), y + (height/2));

        //Secondly, we will get an array of elements that are under this point.
        var _objects:Array = parentApplication.getObjectsUnderPoint(_position);

        //If the length of the objectsList is longer than or equal to 2, we may assume that
        //there is an object
        if(_objects.length >= 2)
        {
            //Set the currentObject variable to the object the cursor is hovering over.
            //The minus two is simple. The cursor is always the last object under that point,
            //so we need the object before that.
            _currentObject = _objects[_objects.length - 2];

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