碰撞系统脚本控制

    获取碰撞检测系统

    默认碰撞检测系统是禁用的,如果需要使用则需要以下方法开启碰撞检测系统

    开启后在运行时可显示 碰撞组件碰撞检测范围,如下图:

    1. manager.enabledDrawBoundingBox = true;

    结果如下图所示:

    碰撞系统脚本控制 - 图2

    1. /**
    2. * 当碰撞产生后,碰撞结束前的情况下,每次计算碰撞结果后调用
    3. * @param {Collider} other 产生碰撞的另一个碰撞组件
    4. * @param {Collider} self 产生碰撞的自身的碰撞组件
    5. */
    6. console.log('on collision stay');
    7. },
    1. collider: cc.BoxCollider
    2. },
    3. start () {
    4. // 开启碰撞检测系统,未开启时无法检测
    5. cc.director.getCollisionManager().enabled = true;
    6. // cc.director.getCollisionManager().enabledDebugDraw = true;
    7. let touchLoc = touch.getLocation();
    8. // https://docs.cocos.com/creator/api/zh/classes/Intersection.html 检测辅助类
    9. if (cc.Intersection.pointInPolygon(touchLoc, this.collider.world.points)) {
    10. console.log("Hit!");
    11. }
    12. else {
    13. console.log("No hit");
    14. }
    15. }, this);

    更多的范例可以到 github 上查看。