title: How to get top mouseEnabled object under point?
body: <p>I use for my flash application external touch-input. Signals from touch panel I receive via TUIO library for as3 ( <a href="https://code.google.com/p/tuio-as3/" rel="nofollow">https://code.google.com/p/tuio-as3/</a> )</p>
  
  <p>I have sometimes problem then TuioManager wrong detects object which must receive touch input.</p>
  
  <pre><code>private function getTopDisplayObjectUnderPoint(point:Point):DisplayObject {
      var targets:Array =  stage.getObjectsUnderPoint(point);
      var item:DisplayObject = (targets.length &gt; 0) ? targets[targets.length - 1] : stage;
  
      if (this.touchTargetDiscoveryMode == TOUCH_TARGET_DISCOVERY_MOUSE_ENABLED) {
          while (targets.length &gt; 0) {
              item = targets.pop() as DisplayObject;
              //ignore debug cursor/object/blob and send object under debug cursor/object/blob
              if ((item is ITuioDebugCursor || item is ITuioDebugBlob || item is ITuioDebugObject || item is ITuioDebugTextSprite) &amp;&amp; targets.length &gt; 0) {
                  continue;
              }
              if (item.parent != null &amp;&amp; !(item is InteractiveObject)) item = item.parent;
              if (item is InteractiveObject) {
                  if ((item as InteractiveObject).mouseEnabled) return item;
              }
          }
          item = stage;
      }
      else if (this.touchTargetDiscoveryMode == TOUCH_TARGET_DISCOVERY_IGNORELIST) {
          while (targets.length &gt; 0) {
              item = targets.pop();
              //ignore debug cursor/object/blob and send object under debug cursor/object/blob
              if ((item is ITuioDebugCursor || item is ITuioDebugBlob || item is ITuioDebugObject || item is ITuioDebugTextSprite) &amp;&amp; targets.length &gt; 0) {
                  continue;
              }
              if (!bubbleListCheck(item)) return item;
          }
          item = stage;
      }
  
      return item;
  }
  </code></pre>
  
  <p>I used <code>this.touchTargetDiscoveryMode == TOUCH_TARGET_DISCOVERY_MOUSE_ENABLED</code>.</p>
  
  <p>This function does not use <code>mouseChildren</code> property.</p>
  
  <p>For example, I use world map in fxg graphic and I want disable all its children: <code>worldMap.mouseChildren = false;</code></p>
  
  <p>But it works if I use system mouse input, but not for TUIO. Result of function <code>getTopDisplayObjectUnderPoint</code> will be one of child MovieClip. In trace I see hierarchy like as: </p>
  
  <pre><code>worldMap
      MovieClip
          ....
             MovieClip
  </code></pre>
  
  <p>Yes, I can set recursively all property <code>mouseEnabled = false</code> for children of <code>worldMap</code>. It can fix for only this problem, but not problem with wrong object selection in more global case.</p>
  
  <p>I tried to modify this function and wrote two helper function:</p>
  
  <pre><code>private function isMouseChildrenEnabled(obj:DisplayObject):Boolean {
      if (obj.parent == null) {
          return true;
      }
      return obj.parent.mouseChildren &amp;&amp; isMouseChildrenEnabled(obj.parent);
  }
  
  private function findMouseEnabledObject(obj:InteractiveObject):InteractiveObject
  {
      if (obj.mouseEnabled &amp;&amp; isMouseChildrenEnabled(obj))
          return obj;
      return obj.parent ? findMouseEnabledObject(obj.parent) : null;
  }
  
  private function getTopDisplayObjectUnderPoint(point:Point):DisplayObject {
      var targets:Array =  stage.getObjectsUnderPoint(point);
      var item:DisplayObject = (targets.length &gt; 0) ? targets[targets.length - 1] : stage;
  
      if(this.touchTargetDiscoveryMode == TOUCH_TARGET_DISCOVERY_MOUSE_ENABLED){
          while(targets.length &gt; 0) {
              item = targets.pop() as DisplayObject;
              //ignore debug cursor/object/blob and send object under debug cursor/object/blob
              if((item is ITuioDebugCursor || item is ITuioDebugBlob || item is ITuioDebugObject || item is ITuioDebugTextSprite) &amp;&amp; targets.length &gt; 0){
                  continue;
              }
              if (item.parent != null &amp;&amp; !(item is InteractiveObject)) item = item.parent;
              if (item is InteractiveObject) {
                  var io:InteractiveObject = findMouseEnabledObject(item as InteractiveObject);
                  if (io) return io;
              }
          }
          item = stage;
      } else if (this.touchTargetDiscoveryMode == TOUCH_TARGET_DISCOVERY_IGNORELIST) {
          while(targets.length &gt; 0) {
              item = targets.pop();
              //ignore debug cursor/object/blob and send object under debug cursor/object/blob
              if((item is ITuioDebugCursor || item is ITuioDebugBlob || item is ITuioDebugObject || item is ITuioDebugTextSprite) &amp;&amp; targets.length &gt; 0){
                  continue;
              }
              if (!bubbleListCheck(item)) return item;
          }
          item = stage;
      }
  
      return item;
  }
  </code></pre>
  
  <p>But after new fixes we have new bugs :(
  I see bug if I use flex drag drop. During drag drop flex shows dragged image and tuio detects wrong drop target. In trace <strong>stage.getObjectsUnderPoint()</strong> I see something like as:</p>
  
  <pre><code>[n] - image (mouseEnabled=true, mouseChildren=true)
     parent: embed_swf_drag_cursor... (mouseEnabled=false, mouseChildren=false)
        parent: application_systemManager... (mouseEnabled=true, mouseChildren=true)
            parent: stage
  [n-1] - borderContainer (mouseEnabled=true, mouseChildren=true) - it is my drop target
  </code></pre>
  
  <p>In result I have systemManager as target, but I need BorderContainer.</p>
  
  <p>How correct find enabled object for user input?</p>
  
  <p>P.S. Tested on Linux and Windows flash players.</p>
  
  <p><em>Update 13.11</em></p>
  
  <p>I tried to write function which get enabled object under point:</p>
  
  <pre><code>private function getTopEnabledObject(object:DisplayObjectContainer, hitPoint:Point):InteractiveObject
  {
      if (!object.mouseChildren)
          return  null;
      var child:DisplayObject;
      for (var i:int = object.numChildren - 1; i &gt;= 0; i--)
      {
          child = object.getChildAt(i);
          if (child.visible &amp;&amp; child is InteractiveObject &amp;&amp; child.hitTestPoint(hitPoint.x, hitPoint.y, true))
          {
              if (child is DisplayObjectContainer)
              {
                  var target:InteractiveObject = getTopEnabledObject(child as DisplayObjectContainer, hitPoint);
                  if (target)
                      return target;
              }
              if ((child as InteractiveObject).mouseEnabled)
                  return child as InteractiveObject; 
          }
      }
      return null;
  }
  </code></pre>
  
  <p>It works slowly in 5-10 times. And I had crashes after many-many touches in short time.</p>