AS3:HitTest任何对象

时间:2011-04-06 14:01:12

标签: flash hittest flashbuilder4

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

2 个答案:

答案 0 :(得分:1)

将要监视的元素放在一个单独的数组中“悬停”, 然后将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);
  }
}

答案 1 :(得分:1)

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

        //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);
        }