圖片一般是正方形,x、y座標一般都是左上角,所以碰撞公式一般爲
// hitRange 碰撞距離
if(Math.abs(shootball.x-ball.x) < hitRange && Math.abs(shootball.y-ball.y)<hitRange)
{
trace("碰撞了");
}
或者
if(MathUtils.distance(shootball.x,shootball.y,ball.x,ball.y)< hitRange)
{
trace("碰撞了");
}
/**
* 計算距離
* @param x1 點1的x座標
* @param y1 點1的y座標
* @param x2 點2的x座標
* @param y2 點2的y座標
* @return 2點之間的距離
*/
public static function distance(x1:Number, y1:Number, x2:Number, y2:Number):Number
{
return Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
}