Adobe Air & Adobe Flex & ActionScript & Mobile Dev & HTML5 & RIA & User Experience
/** * 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:
Leave a reply
Pingbacks & Trackbacks
《Running Panda》移动游戏开发心得分享 – 入门 : : 9RIA.Com 天地会博客聚合
十月 8th, 2011 at 12:02 下午