安陆网站的建设,上海网站建设q.479185700棒,优化的意思,长春网站开发培训碰撞检测文档
刚体自行选择#xff0c;刚体正常设置分组、tag#xff0c;tag用于区分是哪个物体被碰撞了 正常在一个node下挂载脚本就行
注意#xff1a;Builtin 2D 物理模块只会发送 BEGIN_CONTACT 和 END_CONTACT 回调消息。ccclass(TestContactCallBack)
export class …碰撞检测文档
刚体自行选择刚体正常设置分组、tagtag用于区分是哪个物体被碰撞了 正常在一个node下挂载脚本就行
注意Builtin 2D 物理模块只会发送 BEGIN_CONTACT 和 END_CONTACT 回调消息。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);collider.on(Contact2DType.PRE_SOLVE, this.onPreSolve, this);collider.on(Contact2DType.POST_SOLVE, this.onPostSolve, this);}// 注册全局碰撞回调函数if (PhysicsSystem2D.instance) {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);}}onBeginContact (selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {// 只在两个碰撞体开始接触时被调用一次console.log(onBeginContact);}
1、需要注意的是 刚体不能两个都是static否则不会触发碰撞回调
2、碰撞回调的时候不能直接进行node销毁否则会奔溃 onBeginContact (selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {// 只在两个碰撞体开始接触时被调用一次console.log(onBeginContact222otherCollider.name);console.log(onBeginContact222name:selfCollider.name);// console.log(onBeginContact222tag:selfCollider.tag);if(selfCollider.tag10 otherCollider.tag20){setTimeout(() {if(this.node this.node.isValid) {selfCollider.node.destroy();otherCollider.node.destroy();}}, 500);}