區(qū)域檢測
兩個對象之間碰撞使用hitTestPoint并不能很好的檢測區(qū)域碰撞煤伟,用下面代碼來進行高速檢測
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