有两个问题:
第一个是为啥碰撞检测的时候self是另一个类,other才是自己。
第二个就是destroy other,也就是销毁自己bullet这个类的时候,总是报这种错
Uncaught TypeError: Cannot read properties of null (reading ‘SetActive’)
at b2RigidBody2D.setActive (rigid-body.ts:197:21)
at b2RigidBody2D.onEnable (rigid-body.ts:54:14)
at RigidBody2D.onEnable (rigid-body-2d.ts:506:24)
at OneOffInvoker.invokeOnEnable [as _invoke] (component-scheduler.ts:313:18)
at OneOffInvoker.invoke (component-scheduler.ts:143:14)
at NodeActivator.activateNode (node-activator.ts:182:27)
at Node._onHierarchyChangedBase (base-node.ts:1331:46)
at Node._onHierarchyChanged (node.ts:468:9)
at Node.setParent (base-node.ts:449:14)
at Node.setParent (node.ts:435:9)
setActive @ rigid-body.ts:197
onEnable @ rigid-body.ts:54
onEnable @ rigid-body-2d.ts:506
invokeOnEnable @ component-scheduler.ts:313
invoke @ component-scheduler.ts:143
activateNode @ node-activator.ts:182
_onHierarchyChangedBase @ base-node.ts:1331
_onHierarchyChanged @ node.ts:468
setParent @ base-node.ts:449
setParent @ node.ts:435
(anonymous) @ PlayerControl.ts:26
trigger @ scheduler.ts:300
update @ scheduler.ts:266
update @ scheduler.ts:464
tick @ director.ts:703
_updateCallback @ game.ts:947
updateCallback @ pacer-web.ts:65
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:62
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:62
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:62
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:62
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:62
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:62
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:62
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:62
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:62
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:62
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:62
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:62
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:62
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:62
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:62
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:62
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:62
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:62
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:62
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:62
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:62
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:62
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:62
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:62
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:62
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:62
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:62
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:62
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:62
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:62
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:62
requestAnimationFrame (async)
updateCallback @ pacer-web.ts:62
import { EnermyControl } from './EnermyControl';
import { _decorator, Component, Node, Vec3, Collider2D, Contact2DType, IPhysics2DContact, PhysicsSystem2D, BoxCollider2D, EPhysics2DDrawFlags, RigidBody2D } from 'cc';
const { ccclass, property } = _decorator;
@ccclass('BulletControl')
export class BulletControl extends Component {
@property
speed: number = 800;
start() {
// let collider = this.getComponent(BoxCollider2D);
// 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);
// }
// 注册全局碰撞回调函数
// PhysicsSystem2D.instance.enable = true;
// PhysicsSystem2D.instance.debugDrawFlags = EPhysics2DDrawFlags.All
// PhysicsSystem2D.instance.debugDrawFlags = EPhysics2DDrawFlags.Aabb |
// EPhysics2DDrawFlags.Pair |
// EPhysics2DDrawFlags.CenterOfMass |
// EPhysics2DDrawFlags.Joint |
// EPhysics2DDrawFlags.Shape;
if (PhysicsSystem2D.instance) {
// console.log("sf");
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);
}
}
update(deltaTime: number) {
let pos = new Vec3();
this.node.getPosition(pos);
pos.y += deltaTime * this.speed;
if(pos.y > 850){
this.node.destroy;
}
this.node.setPosition(pos);
}
onBeginContact (selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
// 只在两个碰撞体开始接触时被调用一次
if(selfCollider.tag == 0){
console.log(otherCollider.tag+ "self"+ selfCollider.tag);
} else {
selfCollider.getComponent(EnermyControl).die(); // 敌人
otherCollider.node.destroy(); // 自己
}
}
onEndContact (selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
}
onPreSolve (selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
// 每次将要处理碰撞体接触逻辑时被调用
// console.log('onPreSolve');
// this.destroy();
}
onPostSolve (selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
// 每次处理完碰撞体接触逻辑时被调用
// console.log('onPostSolve');
// this.destroy();
}
// onCollisionEnter(other){
// if(other.tag == 1){
// other.getComponent(EnermyControl).die();
// this.node.destroy();
// }
// }
}