先给物体添加collider
onLoad() {
//开启碰撞
cc.director.getCollisionManager().enabled = true;
//绘制碰撞盒
cc.director.getCollisionManager().enabledDebugDraw = true;
//开启物理碰撞
cc.director.getPhysicsManager().enabled = true;
//绘制碰撞盒,数值设为1为开启
cc.director.getPhysicsManager().debugDrawFlags = 0;
},
start() {
},
//碰撞产生时调用
onCollisionEnter: function (other, self) {
//other:碰撞物体的碰撞组件,self:自身的碰撞组件
//判断碰撞物体的group
if (other.node.group === "player") {
}
//判断自身碰撞体所在分组
if(self.tag==="1"){
}
},
//碰撞保持时调用
onCollisionStay: function (other, self) {
},
//碰撞结束时调用
onCollisionExit: function (other, self) {
},
//刚体物理碰撞回调,要先在 rigidbody 中 开启碰撞监听,才会有相应的回调产生。
// 只在两个碰撞体开始接触时被调用一次
onBeginContact: function (contact, selfCollider, otherCollider) {
},
// 只在两个碰撞体结束接触时被调用一次
onEndContact: function (contact, selfCollider, otherCollider) {
},
// 每次将要处理碰撞体接触逻辑时被调用
onPreSolve: function (contact, selfCollider, otherCollider) {
},
// 每次处理完碰撞体接触逻辑时被调用
onPostSolve: function (contact, selfCollider, otherCollider) {
}
// update (dt) {},