box2d节点删除疑惑

拿合成大西瓜练练手,但在使用物理系统时遇到问题,就是在两个水果碰撞到时,我想将想碰撞的两个水果都移除了,但目前只会移除一个,后面的代码不执行也不会报错。
在onBeginContact ()方法里面只会输出 =========1
后面的=========2就没有输出了。
求解。
private createFruit(pos:Vec2):void{

    let fruit=instantiate(this.fprefab);

    this.node.addChild(fruit);

   

    //坐标转换,将场景坐标转为世界坐标,再转为节点内坐标

    let wpos=this.uicamere.getComponent(Camera).screenToWorld(new Vec3(pos.x,pos.y,0));

    let fpos=this.getComponent(UITransform).convertToNodeSpaceAR(wpos);

    fpos.z=0;

    fruit.setPosition(fpos);

    let collider = fruit.getComponent(Collider2D);

    collider.tag=2;

    if (collider) {

        collider.once(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);

    }

   

}

onBeginContact (selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {

    // 只在两个碰撞体开始接触时被调用一次

    //console.log('onBeginContact');

    if(selfCollider.tag===otherCollider.tag){

        console.log("==========1");

        otherCollider.node.removeFromParent();//目前只会执行到这里

        console.log("==========2");

        selfCollider.node.removeFromParent();

    }

}

box2d 在contact 回调中不建议操作节点树。
建议await一帧操作。
我猜应该是这个问题。

1赞