CocosCreator Collision Recognition How to operate Collision Recognition Based on Group

Cocos3 collision group identification problem

Sometimes we need an object to collide with multiple other collision groups

Need to distinguish between collision events

But if we use group to distinguish

The number corresponding to this group is not the number of the group, the power of the group of 2

For example, for the collider numbered 3

Then his recognized number should be 2^3, which is 8, before it can be recognized

 as the picture shows

 so that it can be distinguished

Note that it is a power rather than a number directly

 /**离开碰撞 */
    onEndContact(selfCollider: Collider2D, otherCollider: Collider2D) {
        if (otherCollider.group == 16) {
            //如果处于可放置状态,但是进入了别人的碰撞体,那就加入到数组
            if (this.NowContactUnits.indexOf(otherCollider.node) >= 0) {
                let node: Node;  //要移除的对象
                node = otherCollider.node
                //下面这句话 就是让这个数组元素中 条件通过的 就通过 然后先形成一个新的数组 再赋值
                this.NowContactUnits = this.NowContactUnits.filter(item => item !== node);
            }
            if (this.NowContactUnits.length <= 0) {
                console.log('可以建造')
                this.ChangeBuildState(true)
            }


            //如果离开了别人的碰撞体 则从数组中移除

            //如果数组为0  则可以建造
        }
    }

Guess you like

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