懵逼了,3.0以下的2个物体只加collider就能出发碰撞事件回调(用Builtin 模块也没得用),3.0.的需要怎么改,可以有回调,试了文档没成功~~
import { _decorator, Component, Node, Collider2D, Contact2DType, PhysicsSystem2D, EventTouch } from ‘cc’;
import { DEBUG } from ‘cce.env’;
const { ccclass, property } = _decorator;
@ccclass(‘Test’)
export class Test extends Component {
/* class member could be defined like this */
// dummy = ‘’;
/* use `property` decorator if your want the member to be serializable */
// @property
// serializableDummy = 0;
start () {
//3.0以下
// cc.director.getCollisionManager().enabled=true;
// cc.director.getCollisionManager().enabledDebugDraw = true;
// this.node.on(cc.Node.EventType.TOUCH_MOVE, function (event:cc.Event.EventTouch ) {
// var delta = event.touch.getDelta();
// this.x += delta.x;
// this.y += delta.y;
// },
// this.node);
//3.0+
let self = this;
this.node.on( Node.EventType.TOUCH_MOVE, (event:EventTouch) =>{
let delta = event.touch.getDelta();
let x= this.node.position.x;
let y= this.node.position.y;
this.node.setPosition(x+=delta.x,y+=delta.y,0);
} ,this)
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);
}
}
//#region 3.0++
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');
}
//#endregion
//#region 3.0以下
onCollisionEnter(other:any,self:any){
console.log("进入",other);
console.log("进入",self);
}
onCollisionStay(other:any,self:any){
console.log("保持",other);
console.log("保持",self);
}
onCollisionExit(other:any,self:any){
console.log("离开",other);
console.log("离开",self);
}
//#endregion
}
