Cocos Creator 随笔(2)-- 不规则碰撞组件polygonCollider

监测鼠标点击不规则区域

/**
 * 多边形点击响应区域
 */
private collider: cc.PolygonCollider; //不规则碰撞组件

let monitor = this.node.getChildByName("monitor"); //图片
this.collider = monitor.getComponent(cc.PolygonCollider); //获去碰撞组件
if (this.collider) {
     monitor.on(cc.Node.EventType.TOUCH_START, this.onAreaClick, this);
}
        
onAreaClick(event: any): void {
        let touchLoc = event.getLocation();
        let target = event.getCurrentTarget();
        let pos = target.convertToNodeSpaceAR(touchLoc);

        if (cc.Intersection.pointInPolygon(pos, this.collider.points)) {
            Debugger.Log("ok 在点击区域内");
        } 
        else {
            Debugger.Log("抱歉 超出点击区域");
        }
    }

你可能感兴趣的:(Cocos)