只有加入碰撞组件后才能产生碰撞
碰撞组件有三种:
第一种碰撞组件加入后出现方形的绿色线框,可以在组件中编辑碰撞区域的大小和偏移,框选“编辑”后可以手动调整碰撞区域大小,“Tag”给不同的碰撞体编号,用来区分不同的碰撞。
第二种碰撞组件是圆形碰撞,第三种碰撞组件是多边形碰撞
脚本部分:
cc.director.getCollisionManager().enabled = true;//开启碰撞检测,默认不开启,写在脚本生命周期内
cc.director.getCollisionManager().enabled = true;//开启碰撞检测,默认不开启,写在脚本生命周期内
onCollisionEnter(other,self){
console.debug("撞到了"+other.tag);
}
onCollisionStay(other,self){
console.debug("持续碰撞")
}
onCollisionExit(other,self){
console.debug("不撞了");
}
onCollisionEnter(other,self){
console.debug("撞到了"+other.tag);
}
onCollisionStay(other,self){
console.debug("持续碰撞")
}
onCollisionExit(other,self){
console.debug("不撞了");
}
//写在脚本生命周期外,三个脚本分别是碰撞开始、碰撞持续和碰撞结束,括号里前面的是其他碰撞体,后面的是本碰撞体。