AS3透明碰撞 自动判别图片透明区域碰撞

package helper
{
	import com.pblabs.engine.PBE;

	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.geom.ColorTransform;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;

	/**
	 * 矩形碰撞检测
	 * */
	public class HH_CollisionDetection
	{
		public function HH_CollisionDetection()
		{
		}

		/**
		 * param p_clip1 元件1
		 * param p_clip2 元件2
		 * param p_alphaTolerance
		 * return
		 * */
		public static function checkForCollision(p_clip1:*, p_clip2:*, p_alphaTolerance:Number=250):Rectangle
		{
			// 获取对象坐标区域
			var bounds1:Object=p_clip1.getBounds(PBE.mainStage);
			var bounds2:Object=p_clip2.getBounds(PBE.mainStage);

			// 检测两矩形是否碰撞
			if (((bounds1.right < bounds2.x) || (bounds2.right < bounds1.x)) || ((bounds1.bottom < bounds2.y) || (bounds2.bottom < bounds1.y)))
			{
				return null;
			}
			// 区域测试:
			var bounds:Object={};
			bounds.x=Math.max(bounds1.x, bounds2.x);
			bounds.right=Math.min(bounds1.right, bounds2.right);
			bounds.y=Math.max(bounds1.y, bounds2.y);
			bounds.bottom=Math.min(bounds1.bottom, bounds2.bottom);

			var img:BitmapData=new BitmapData(bounds.right - bounds.x, bounds.bottom - bounds.y, false);

			var mat:Matrix=p_clip1.transform.concatenatedMatrix;
			mat.tx-=bounds.x;
			mat.ty-=bounds.y;
			img.draw(p_clip1, mat, new ColorTransform(1, 1, 1, 1, 255, -255, -255, p_alphaTolerance));

			mat=p_clip2.transform.concatenatedMatrix;
			mat.tx-=bounds.x;
			mat.ty-=bounds.y;
			img.draw(p_clip2, mat, new ColorTransform(1, 1, 1, 1, 255, 255, 255, p_alphaTolerance), "difference");

			var intersection:Rectangle=img.getColorBoundsRect(0xFFFFFFFF, 0xFF00FFFF);

			if (intersection.width == 0)
			{
				return null;
			}

			intersection.x+=bounds.x;
			intersection.y+=bounds.y;

			return intersection;
		}

	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值