解决方案使用的是碰撞组件PolygonCollider
代码如下:
cc.Class({
extends: cc.Component,
properties: {
spriteA: cc.Node,
},
// use this for initialization
onLoad: function () {
//开启碰撞判断
cc.director.getCollisionManager().enabled = true;
//开启碰撞debugDraw
cc.director.getCollisionManager().enabledDebugDraw = true;
this.spriteA.on(cc.Node.EventType.TOUCH_START, this.onTouchBegin, this);
},
onDestroy() {
this.spriteA.off(cc.Node.EventType.TOUCH_START, this.onTouchBegin, this);
},
onTouchBegin(event) {
//世界坐标系点击判断
// var pos = event.getLocation();
// let polygonCollider = event.target.getComponent(cc.PolygonCollider);
// if (polygonCollider) {
// let result = cc.Intersection.pointInPolygon(pos, polygonCollider.world.points);
// console.log("碰撞组件的点击测试" + result)
// }
//node坐标系点击判断
var pos = event.getLocation();
let hitPos = event.target.convertToNodeSpaceAR(pos);
let polygonCollider = event.target.getComponent(cc.PolygonCollider);
if (polygonCollider) {
let result = cc.Intersection.pointInPolygon(hitPos, polygonCollider.points);
console.log("碰撞组件的点击测试" + result)
}
},
update: function (dt) {
}
});