【问题标题】:AS3: HitTest Any ObjectAS3:HitTest 任何对象
【发布时间】:2011-04-06 14:01:11
【问题描述】:

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

【问题讨论】:

  • 你真的需要对图像本身进行命中测试吗?鼠标还在,只是看不到而已。你可以用那个 hittest/mouseOver
  • 鼠标不存在,这就是问题所在;)我正在使用不同的方法来控制图像:)
  • 你是如何控制光标的?
  • 哈哈,好吧:D。我看你还是修好了,祝你好运!

标签: flash hittest flashbuilder4


【解决方案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);
  }
}

【讨论】:

  • 我之前尝试过,但无法使用它,因为有些对象不是光标的子对象 :) 还是谢谢
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-25
    相关资源
    最近更新 更多