Egret 碰撞检测总结

1 点与对象的碰撞

bmp.hitTestPoint(pointX,pointY) ;非精确碰撞

bmp.hitTestPoint(pointX,pointY,true) ;精确碰撞 // 很耗性能

2 对象与对象碰撞 shp1和 shp2

对象与对象碰撞 egret中 是用的 盒子碰撞 intersects

//两物品重叠的碰撞判断方式

        public static hitTest(obj1:egret.DisplayObject,obj2:egret.DisplayObject):boolean
        {
            var rect1: egret.Rectangle = obj1.getBounds();
            var rect2: egret.Rectangle = obj2.getBounds();
            rect1.x = obj1.x;
            rect1.y = obj1.y;
            rect2.x = obj2.x;
            rect2.y = obj2.y;
            return rect1.intersects(rect2);
        }

3. 圆形碰撞

let fromX=1;
let fromY=1;
let toX=10;
let toY=10;
let from=new egret.Point(fromX,fromY);
let to=new egret.Point(toX,toY);
let distance=egret.Point.distance(from,to);


if(distance<=width1/2+width2/2){
//todo 撞了
}

通过计算圆心之间的距离进行检测是否碰撞

猜你喜欢

转载自blog.csdn.net/jiangguilong2000/article/details/80483585