egret 区域检测

区域检测

两个对象之间碰撞使用hitTestPoint并不能很好的检测区域碰撞,用下面代码来进行高速检测

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);
}

距离检测

两个点的距离检测, 效率再高一点可以不做距离检测,做距离平方检测

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 撞了
}

参考:https://blog.csdn.net/jiangguilong2000/article/details/80483585

推荐阅读更多精彩内容