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

import { _decorator, Component, Node, BoxCollider2D, Contact2DType, PhysicsSystem2D, Collider2D, IPhysics2DContact } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('Player')
export class Player extends Component {

    start() {

        // 注册单个碰撞体的回调函数
        let collider = this.getComponent(BoxCollider2D);
        if (collider) {
            // Builtin 2D 物理模块只会发送 BEGIN_CONTACT 和 END_CONTACT 回调消息。
            collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
            collider.on(Contact2DType.END_CONTACT, this.onEndContact, this);
            // collider.on(Contact2DType.PRE_SOLVE, this.onPreSolve, this);
            // collider.on(Contact2DType.POST_SOLVE, this.onPostSolve, this);
        }

        // 注册全局碰撞回调函数
        if (PhysicsSystem2D.instance) {
            // Builtin 2D 物理模块只会发送 BEGIN_CONTACT 和 END_CONTACT 回调消息。
            PhysicsSystem2D.instance.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
            PhysicsSystem2D.instance.on(Contact2DType.END_CONTACT, this.onEndContact, this);
            // PhysicsSystem2D.instance.on(Contact2DType.PRE_SOLVE, this.onPreSolve, this);
            // PhysicsSystem2D.instance.on(Contact2DType.POST_SOLVE, this.onPostSolve, this);
        }
    }

    /**
     * 只在两个碰撞体开始接触时被调用一次
     * @param selfCollider 指的是回调脚本的节点上的碰撞体
     * @param otherCollider 指的是发生碰撞的另一个碰撞体
     * @param contact 碰撞主要的信息, 位置和法向量, 带有刚体的本地坐标来, 
     */
    onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
        console.log('onBeginContact');

        // contact.getWorldManifold在 Builtin 物理模块这个参数为空
        // const worldManifold = contact.getWorldManifold();
        // // 碰撞点数组
        // const points = worldManifold.points;
        // // 碰撞点上的法向量
        // const normal = worldManifold.normal;
    }
    onEndContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
        // 只在两个碰撞体结束接触时被调用一次
        console.log('onEndContact');
    }
    onPreSolve(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
        // 每次将要处理碰撞体接触逻辑时被调用
        console.log('onPreSolve');
    }
    onPostSolve(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
        // 每次处理完碰撞体接触逻辑时被调用
        console.log('onPostSolve');
    }
}

测试

当角色和金币相撞时触发

其他

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

碰撞后消失的两种方法

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

CocosCreator实现的 解救人质 游戏,学会碰撞检测rescue.7z // Bullet.js cc.Class({ extends: cc.Component, properties: { mSpeed: 300, }, // LIFE-CYCLE CALLBACKS: // onLoad () {}, start() { var manager = cc.director.getCollisionManager(); // 获取碰撞检测系统 manager.enabled = true; }, update(dt) { // 设置子弹移动,当超出屏幕范围未发生碰撞时自动销毁 this.node.y += this.mSpeed * dt; if (this.node.y > 580) { console.log('超出屏幕范围,子弹销毁!'); this.node.destroy(); } }, /** * 当碰撞产生的时候调用 * @param {Collider} other 产生碰撞的另一个碰撞组件 * @param {Collider} self 产生碰撞的自身的碰撞组件 */ onCollisionEnter: function (other, self) { console.log('on collision enter'); if (other.tag == 1) { // 子弹碰到人质时,解救失败! console.log('解救人质失败!'); var failLabel = this.node.parent.getChildByName('failLabel'); failLabel.active = true; this.node.destroy(); } else if (other.tag == 2) { // 子弹碰到敌人时,解救成功! console.log('解救人质成功!'); var successLabel = this.node.parent.getChildByName('successLabel'); successLabel.active = true; this.node.destroy(); } }, /** * 当碰撞产生后,碰撞结束前的情况下,每次计算碰撞结果后调用 * @param {Collider} other 产生碰撞的另一个碰撞组件 * @param {Collider} self 产生碰撞的自身的碰撞组件 */ onCollisionStay: function (other, self) { console.log('on collision stay'); }, /** * 当碰撞结束后调用 * @param {Collider} other 产生碰撞的另一个碰撞组件 * @param {Collider} self 产生碰撞的自身的碰撞组件 */ onCollisionExit: function (other, self) { console.log('on collision exit'); } });
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

瑶山

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值