cocos这个刚体AllowSleep睡眠属性真的服了,cocos的坑是真多啊

这个属性说的是:如果此刚体永远都不应该进入睡眠,那么设置这个属性为 False。需要注意这将使 CPU 占用率提高,文档地址:Cocos Creator API

休眠:意思应该就是只展示ui效果,没有碰撞检测了,我的老天,cocos的人怎么想的,我为什么要使用刚体你不知道吗?我就是需要碰撞检测啊,你现在到好,直接给我睡眠,还是默认的,我怎么说搞了那么久,只有子弹发射出来的一下有碰撞,后面就没有了..........真的不至于默认给true啊,应该默认给false,等别人真的需要只检测一下的时候,再来设置为ture啊

还有这个碰撞回掉......我肯定是想使用碰撞的,大部分情况都是要回掉的吧,结果你到好,又给了默认false........需要勾选为true才有回掉函数:

注册回调函数

注册一个碰撞回调函数有两种方式,一种是通过指定的 collider 注册;另一种是通过 2D 物理系统注册一个全局的回调函数。

注意 :Builtin 2D 物理模块只会发送 BEGIN_CONTACTEND_CONTACT 回调消息。

javascript 复制代码
@ccclass('TestContactCallBack')
export class TestContactCallBack extends Component {
    start () {
        // 注册单个碰撞体的回调函数
        let collider = this.getComponent(Collider2D);
        if (collider) {
            collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
            collider.on(Contact2DType.END_CONTACT, this.onEndContact, this);
        }

        // 注册全局碰撞回调函数
        if (PhysicsSystem2D.instance) {
            PhysicsSystem2D.instance.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
            PhysicsSystem2D.instance.on(Contact2DType.END_CONTACT, this.onEndContact, this);
        }
    }
    onBeginContact (selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
        // 只在两个碰撞体开始接触时被调用一次
        console.log('onBeginContact');
    }
    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');
    }
}
相关推荐
kyriewen1116 分钟前
给浏览器画个圈:CSS contain 如何让页面从“卡成PPT”变“丝滑如德芙”
开发语言·前端·javascript·css·chrome·typescript·ecmascript
英俊潇洒美少年29 分钟前
react19和vue3的优缺点 对比
前端·javascript·vue.js·react.js
~无忧花开~2 小时前
React生命周期全解析
开发语言·前端·javascript·react.js·前端框架·react
哈__3 小时前
ReactNative项目OpenHarmony三方库集成实战:react-native-maps
javascript·react native·react.js
cj81403 小时前
Prompt,Agent,Skill,Mcp分别于langchain有什么关系
前端
SuperEugene3 小时前
Axios + Vue 错误处理规范:中后台项目实战,统一捕获系统 / 业务 / 接口异常|API 与异步请求规范篇
前端·javascript·vue.js·前端框架·axios
行走的陀螺仪3 小时前
手写 Vue3 极简 i18n
前端·javascript·vue.js·国际化·i18n
羽沢313 小时前
一篇简单的STOMP教程QAQ
前端·javascript·stomp
code_Bo3 小时前
使用AI完成Swagger接口类型在前端自动生成的工具
前端·后端·架构