您当前的位置: 首页 > 

Jave.Lin

暂无认证

  • 8浏览

    0关注

    704博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

AS3 高效像素检测方法(优先,矩形,交集后,再利用通道像素差值混合)

Jave.Lin 发布时间:2012-05-09 11:44:07 ,浏览量:8

主要代码:

package
{
	
	import flash.display.BitmapData;
	import flash.display.BlendMode;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	
	import flash.geom.ColorTransform;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	/**
	 * 总结:Jave.Lin
	 * 
	 * 使用:交集矩形得到的像素(两显示对象的差值混合像素后),
	 * 判断过滤颜色为红色的都过滤掉,如果过滤结果为Rectange.Empty,
	 * 说明没有像素交集,否则说明的像素交集,
	 * 
	 * @author 原贴:http://www.tink.ws/blog/as-30-hittest/
	 */	
	public class HitTest
	{
		
		public static function complexHitTestObject( target1:DisplayObject, target2:DisplayObject,  accurracy:Number = 1 ):Boolean
		{
			return complexIntersectionRectangle( target1, target2, accurracy ).width != 0;
		}
		//先求得两显示对象是否有最外层矩形作用矩形的交集
		public static function intersectionRectangle( target1:DisplayObject, target2:DisplayObject ):Rectangle
		{
			// If either of the items don't have a reference to stage, then they are not in a display list
			// or if a simple hitTestObject is false, they cannot be intersecting.
			if( !target1.root || !target2.root || !target1.hitTestObject( target2 ) ) return new Rectangle();
			
			// Get the bounds of each DisplayObject.
			var bounds1:Rectangle = target1.getBounds( target1.root );
			var bounds2:Rectangle = target2.getBounds( target2.root );
			
			// Determine test area boundaries.
			var intersection:Rectangle = new Rectangle();
			intersection.x   = Math.max( bounds1.x, bounds2.x );
			intersection.y    = Math.max( bounds1.y, bounds2.y );
			intersection.width      = Math.min( ( bounds1.x + bounds1.width ) - intersection.x, ( bounds2.x + bounds2.width ) - intersection.x );
			intersection.height = Math.min( ( bounds1.y + bounds1.height ) - intersection.y, ( bounds2.y + bounds2.height ) - intersection.y );
			
			return intersection;
		}
		
		public static function complexIntersectionRectangle( target1:DisplayObject, target2:DisplayObject, accurracy:Number = 1 ):Rectangle
		{                     
			if( accurracy             
关注
打赏
1664331872
查看更多评论
0.0651s