您的位置:首页 > 其它

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

2011-10-14 12:45 375 查看
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;
}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: