cocos creator 碰撞检测

1.普通的碰撞检测

a.给精灵添加boxCollision

 

b.代码里面开启碰撞管理

cc.director.getCollisionManager().enabled = true;//开启碰撞功能cc.director.getCollisionManager().enabledDebugDraw = true;//开启调试功能,碰撞体上面会有碰撞范围

c.碰撞回调函数

/**
 * 当碰撞产生的时候调用
 * @param  {Collider} other 产生碰撞的另一个碰撞组件
 * @param  {Collider} self  产生碰撞的自身的碰撞组件
 */
onCollisionEnter(other, self){}
onCollisionStay(other, self){}
onCollisionExit(other, self){}

2.物理引擎的碰撞检测

a.添加刚体和刚体碰撞

b.开启物理系统 和碰撞

cc.director.getPhysicsManager().enabled = true;
cc.director.getCollisionManager().enabled = true;

c.设置是否需要回调

this.rigidBody  = this.node.getComponent(cc.RigidBody);
this.rigidBody.enabledContactListener = true;

或者面板设置

d.回调函数


 // 只在两个碰撞体开始接触时被调用一次
    onBeginContact: function (contact, selfCollider, otherCollider) {
    },

    // 只在两个碰撞体结束接触时被调用一次
    onEndContact: function (contact, selfCollider, otherCollider) {
    },

    // 每次将要处理碰撞体接触逻辑时被调用
    onPreSolve: function (contact, selfCollider, otherCollider) {
    },

    // 每次处理完碰撞体接触逻辑时被调用
    onPostSolve: function (contact, selfCollider, otherCollider) {
    }

 

注意:开启碰撞管理功能和物理引擎功能必须放在最前面,即代码的最前面,最好是放在canvas上面

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>