• CocosCreator-3.6 三步解决2D碰撞监听


    目录

    一,开启碰撞监听

    二,开启碰撞回调

    三,注册回调函数

    测试

    其他


    之前做的是2.x版本的,最近切到了3.6,记录一下

    一,开启碰撞监听

    项目-项目设置-功能剪裁

     Box2D 物理模块需要先在 Rigidbody 中 开启碰撞监听,才会有相应的回调产生。本文只监听,不产生实际碰撞,选择了内置2D,没有添加Rigidbody2D组件

    二,开启碰撞回调

    节点添加BoxCollider2D组件

    • Builtin 物理模块只需要有碰撞体组件就可以产生碰撞回调。(本文)
      • Sensor指明碰撞组件是否为传感器类型,传感器类型的碰撞组件会产生碰撞回调,但是不会发生物理碰撞效果PS:脚本挂在勾选了此项的节点这里
    • Box2D开启方法为,在 Rigidbody2D 的 属性检查器 勾选 EnabledContactListener 属性(需要添加Rigidbody2D组件)

    三,注册回调函数

    注册方式:

    1. 通过指定的 collider 注册
    2. 通过 2D 物理系统注册一个全局的回调函数

    Builtin 2D 物理模块只会发送 BEGIN_CONTACT 和 END_CONTACT 回调消息

    创建脚本Player.ts

    1. import { _decorator, Component, Node, BoxCollider2D, Contact2DType, PhysicsSystem2D, Collider2D, IPhysics2DContact } from 'cc';
    2. const { ccclass, property } = _decorator;
    3. @ccclass('Player')
    4. export class Player extends Component {
    5. start() {
    6. // 注册单个碰撞体的回调函数
    7. let collider = this.getComponent(BoxCollider2D);
    8. if (collider) {
    9. // Builtin 2D 物理模块只会发送 BEGIN_CONTACT 和 END_CONTACT 回调消息。
    10. collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
    11. collider.on(Contact2DType.END_CONTACT, this.onEndContact, this);
    12. // collider.on(Contact2DType.PRE_SOLVE, this.onPreSolve, this);
    13. // collider.on(Contact2DType.POST_SOLVE, this.onPostSolve, this);
    14. }
    15. // 注册全局碰撞回调函数
    16. if (PhysicsSystem2D.instance) {
    17. // Builtin 2D 物理模块只会发送 BEGIN_CONTACT 和 END_CONTACT 回调消息。
    18. PhysicsSystem2D.instance.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
    19. PhysicsSystem2D.instance.on(Contact2DType.END_CONTACT, this.onEndContact, this);
    20. // PhysicsSystem2D.instance.on(Contact2DType.PRE_SOLVE, this.onPreSolve, this);
    21. // PhysicsSystem2D.instance.on(Contact2DType.POST_SOLVE, this.onPostSolve, this);
    22. }
    23. }
    24. /**
    25. * 只在两个碰撞体开始接触时被调用一次
    26. * @param selfCollider 指的是回调脚本的节点上的碰撞体
    27. * @param otherCollider 指的是发生碰撞的另一个碰撞体
    28. * @param contact 碰撞主要的信息, 位置和法向量, 带有刚体的本地坐标来,
    29. */
    30. onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
    31. console.log('onBeginContact');
    32. // contact.getWorldManifold在 Builtin 物理模块这个参数为空
    33. // const worldManifold = contact.getWorldManifold();
    34. // // 碰撞点数组
    35. // const points = worldManifold.points;
    36. // // 碰撞点上的法向量
    37. // const normal = worldManifold.normal;
    38. }
    39. onEndContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
    40. // 只在两个碰撞体结束接触时被调用一次
    41. console.log('onEndContact');
    42. }
    43. onPreSolve(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
    44. // 每次将要处理碰撞体接触逻辑时被调用
    45. console.log('onPreSolve');
    46. }
    47. onPostSolve(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
    48. // 每次处理完碰撞体接触逻辑时被调用
    49. console.log('onPostSolve');
    50. }
    51. }

    测试

    当角色和金币相撞时触发

    其他

    1. // 节点名字和标签
    2. console.log(otherCollider.node.name);
    3. console.log(otherCollider.tag);

    碰撞后消失的两种方法

    1. if (otherCollider.tag == 3) {
    2. // 不会从内存中释放, 默认传入false, 会清空节点上绑定的事件和 action
    3. // 文档:https://docs.cocos.com/creator/manual/zh/scripting/basic-node-api.html?h=removefromparent
    4. // otherCollider.node.removeFromParent();
    5. // 节点不再使用
    6. otherCollider.node.destroy();
    7. }

  • 相关阅读:
    x64内核实验5-API进0环
    excel中如何使用Replace函数?
    springboot视图渲染技术
    数据恢复软件EasyRecovery支持恢复所有类型的文件
    好马配好鞍:Linux Kernel 4.12 正式发布
    微服务技术栈-初识Docker
    文件操作(Java)
    教你基于MindSpore用DCGAN生成漫画头像
    PNG文件格式-笔记
    自定义qtquick 插件模块,支持qmldesigner
  • 原文地址:https://blog.csdn.net/qq_44695727/article/details/126944258