2011年6月8日 星期三

DisplayObjectContainer.getObjectsUnderPoint()方法_取得指定point座標上的物件

getObjectsUnderPoint()方法
public function getObjectsUnderPoint(point:Point):Array
傳回位於指定點底下,且屬於此 DisplayObjectContainer 實體之子系 (或孫系,以下類推) 的物件陣列。

※此例, 以DisplayObjectContainer.getObjectsUnderPoint()方法, 取得與(200,200)座標重疊的物件陣列.
※aaa、bbb、ccc三個Shape物件配置於元件庫中, 並分別與外部extends Sprite的三個類別AAA、BBB、CCC綁定

1000607_getObjectsUnderPoint.swf


※ trace出來的結果 :
[object Shape],[object Shape],[object TextField]
0 : instance3 : [object Shape]
1 : instance9 : [object Shape]
2 : instance10 : [object TextField]

package  {
 
 import flash.display.MovieClip;
 import flash.geom.Point;
 import flash.display.DisplayObjectContainer;
 import flash.display.Sprite;
 
 public class getObjectsUnderPoint_1000607 extends MovieClip {
  
  public var container:Sprite;
  public var aaa:Sprite;
  public var bbb:Sprite;
  public var ccc:Sprite;
  private var point:Point;
  
  public function getObjectsUnderPoint_1000607() {
   this.container = new Sprite();
   this.addChild(this.container);
   this.createObjects();
   this.point = new Point(200,200);
   var objectsArr:Array = this.getInfo(this.point);
   this.showInfo(objectsArr);
   return;
  }
  
  private function showInfo(_objectsArr:Array):void{
   trace(_objectsArr);
   for (var i:String in _objectsArr){
    trace(i + " : " + _objectsArr[i].name + " : " + _objectsArr[i]);
   }
   /*trace結果
    [object Shape],[object Shape],[object TextField]
    0 : instance3 : [object Shape]
    1 : instance9 : [object Shape]
    2 : instance10 : [object TextField]*/
   return;
  }
  
  private function createObjects():void{
   this.aaa = this.setObjects(aaa,AAA,264,226);
   this.bbb = this.setObjects(bbb,BBB,350,158);
   this.ccc = this.setObjects(ccc,CCC,188,154);
   return;
  }
  
  private function setObjects(_instance:*,_class:Class,_xPos:Number,_yPos:Number):Sprite{
   _instance = new _class();
   _instance.x = _xPos;
   _instance.y = _yPos;
   _instance.name = _instance.toString();
   this.container.addChild(_instance);
   /*trace(_instance);*/
   return _instance;
  }
  
  private function getInfo(_point:Point):Array{
   var objectsArr:Array = this.getObjectsUnderPoint(_point);
   return objectsArr;
  }
 }
 
}

沒有留言: