event.localX MovieClip 与任何其他 DisplayObject 无关 over/under/inside

event.localX of a MovieClip regardless of any other DisplayObject over/under/inside

这是一个例子:

var table:Table = new Table();
stage.addChild(table);
//table covers the whole stage
for (var i:int = 0; i<= 10; i++){
  var book:Book = new Book();
  book.x = Math.random() * stage.stageWidth;
  book.y = Math.random() * stage.stageHeight;
  if (Math.random() < .5){
    stage.addChild(book)
  }
  else {
    table.addChild(book)
  }

stage.addEventListener(MouseEvent.CLICK, clicked);

function clicked(event:MouseEvent){
trace(event.localX, event.localY);
}

这里我需要的是 TABLE 的 localX 或 localY,不需要其他任何东西。

所以一般的问题是"how to return event.localX of a certain MovieClip regardless of any other DisplayObject over/under/inside it, without setting the mouseChildren to false (as I need them to be enabled)"

您可以使用 DisplayObject 的 globalToLocal 方法将 Point 从相对于舞台转换为相对于 table 对象。

function clicked(event:MouseEvent){
    var globalPt:Point = new Point(event.stageX, event.stageY); 
    var tablePt:Point = table.globalToLocal(globalPt);
    trace(tablePt.x, tablePt.y);
}