Skip to content

Instantly share code, notes, and snippets.

@nsdevaraj
Created November 2, 2011 16:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nsdevaraj/1334083 to your computer and use it in GitHub Desktop.
Save nsdevaraj/1334083 to your computer and use it in GitHub Desktop.
Dispatch Mouse Event on any X, Y Coordinates
protected function clickHandler(event:MouseEvent):void
{
var pt:Point = new Point();
pt.x = 201;
pt.y =101;
var ev:MouseEvent = new MouseEvent(MouseEvent.CLICK);
dispatchEventUnderMouse(ev,stage,pt)
}
public static function dispatchEventUnderMouse(event:Event, stage:Stage, point:Point):Boolean {
//return if stage is disabled
if (!stage.mouseEnabled) return false;
//find the topmost object
var cur:DisplayObject = stage.getObjectsUnderPoint(point).pop();
if (cur == null) cur = stage;
//build the path
var path:Array = [];
while (cur != null) {
path.push(cur);
cur = cur.parent;
}
//traverse the path from the root to find the right InteractiveObject
while (path.length > 0) {
cur = path.pop();
if (cur is InteractiveObject) {
if (!(cur as InteractiveObject).mouseEnabled) {
cur = cur.parent;
break;
}
if (cur is DisplayObjectContainer) {
if (!(cur as DisplayObjectContainer).mouseChildren) break;
}
}
else {
cur = cur.parent;
break;
}
}
return cur.dispatchEvent(event);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment