反馈一个物理碰撞的BUG

版本:cocos creator v2.4.0

在使用物理碰撞的时候,两个物体还在接触阶段,但是改变方向却会重复触发onBeginContact和onEndContact。

如图:

Kapture 2020-08-12 at 1.34.26.gif

会出现这种情况的原因是改变了节点的 scaleX。(这个应该是BUG吧?)

代码:


const { ccclass, property } = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    _body;

    start() {
        this._body = this.getComponent(cc.RigidBody);
        this._body.enabledContactListener = true;
        // 测试绘制
        cc.director.getPhysicsManager().debugDrawFlags = cc.PhysicsManager.DrawBits.e_aabbBit |
            cc.PhysicsManager.DrawBits.e_jointBit |
            cc.PhysicsManager.DrawBits.e_shapeBit
            ;


        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
    }

    onBeginContact(contact, selfCollider, otherCollider) {
        cc.log("接触物体");
    }

    onEndContact(contact, selfCollider, otherCollider) {
        cc.log("离开物体");
    }

    onKeyDown(event: cc.Event.EventKeyboard): void {
        switch (event.keyCode) {
            case cc.macro.KEY.left:
            case cc.macro.KEY.d:
                this.moveCtrl(1);
                break;
            case cc.macro.KEY.right:
            case cc.macro.KEY.a:
                this.moveCtrl(-1);
                break;
        }
    }

    onKeyUp(event: cc.Event.EventKeyboard): void {
        let v = this._body.linearVelocity;
        v.x = 0;
        this._body.linearVelocity = v;
    }

    moveCtrl(direct: number) {
        let v = this._body.linearVelocity;
        v.x = 300 * direct;
        this._body.linearVelocity = v;

        // 问题所在:改变水平翻转时,会触发onEndContact和onEndContact,实际上两个物体还是接触状态
        this.node.scaleX = direct;

        cc.log("移动:" + direct);
    }
}


解决了吗, 我也遇到这样的问题