/**
 * Replace Displayobject.hitTestObject function.
 * @author Alvin/AedisJu
 * @blog http://blog.richmediaplus.com
 * @param source Hit DisplayObject Source.
 * @param target Hit DisplayObject Target.
 * @return if test target, return true, else flase.
 */		
public static function hitTestTarget(source:DisplayObject, target:DisplayObject):Boolean
{
	var rtnB:Boolean = false;
	if(source.x <= target.x && target.x <= source.x + source.width)
	{
		if(source.y <= target.y + target.height && target.y + target.height <= source.y + source.height)
			return true;
		if(source.y <= target.y && target.y <= source.y + source.height)
			return true;
	}
	if(target.x <= source.x && source.x <= target.x + target.width)
	{
		if(target.y <= source.y + source.height && source.y + source.height <= target.y+ target.height)
			return true;
		if(target.y <= source.y && source.y <= target.y + target.height)
			return true;
	}
	return rtnB;
}

DisplayObject.hitTestObject(obj:DisplayObject):Boolean
計算顯示對象,以確定它是否與 Obj 顯示對象重疊或相交。
缺點之一是它必須存在于顯示對象列表中。

richmediaplus.utils.CommonUtils
hitTestTarget(source:DisplayObject, target:DisplayObject):Boolean

利用 x, y, width, height 模仿2個矩形之間的重疊和相交檢測,相比 hitTestObject(obj:DisplayObject):Boolean,不在顯示列表就可以進行檢測,在物件隨機排列等方法中用比較常見。

不得不承認,hitTestTarget hitTestObject 效能略低,但個人認為幾乎不影響,10,000個碰撞檢測的結果,平均相差8ms

Related posts:

  1. htmlFormat for Reduce DataBase Storage in Client
  2. Create Trail Information & Expired Date in ActionScript3 & Flex
  3. AS3工程 Custom Preloader 動態加載 Application
  4. TextEvent.LINK 建立安全信任網址跳轉方案
  5. Flex 3 Style Module