CocosCreator3.x 2D collision instance tutorial

Reference document for this article: 2D Physics · Cocos Creator

Those with strong ability can directly see

Because I plan to use cocos to write a small tool for pathfinding, but found that the new version of cocos is actually not the same as 2d, the main differences are as follows

1. Removed the 2d pure collision Collider, all belong to physical collision

2. Although it is like 1, the collision system atmosphere is Butlin (that is, the 2d non-physical collision system in the cocos2.x period) and Box2D collision system (mainly used for physical collisions)

My requirement is to only need collision detection, no physical related, so just set it according to the document

that is here

Then in this place, you can set the collision group, just like cocos2.x

 

    //初始化函数
    InitialBlock() {


        //注册
        // 注册单个碰撞体的回调函数
        let collider = this.getComponent(Collider2D);
        if (collider) {
            //参数1:碰撞类型
            //参数2:回调函数
            //参数3:信息会返回到哪个脚本(比如this就是这个,一般也是this,这里只做猜测,闲得无聊可以看源码)
            collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);

        }
    }
    //回调函数
    onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D) {
        // 只在两个碰撞体开始接触时被调用一次
        console.log('onBeginContact');
    }

 and then put in the relevant nodes

 Then there is output

 end message

Guess you like

Origin blog.csdn.net/Yourset/article/details/123921494