Deprecated: parse_url(): Passing null to parameter #1 ($url) of type string is deprecated in /www/wwwroot/blog_qqvbc_com/usr/plugins/Access/Access_Core.php on line 339

Deprecated: parse_url(): Passing null to parameter #1 ($url) of type string is deprecated in /www/wwwroot/blog_qqvbc_com/usr/plugins/Access/Access_Core.php on line 392

Deprecated: parse_url(): Passing null to parameter #1 ($url) of type string is deprecated in /www/wwwroot/blog_qqvbc_com/usr/plugins/Access/Access_Core.php on line 394
Cocos Creator 主动检测碰撞(3.8.8适用) - Joyber 的博客

Cocos Creator 主动检测碰撞(3.8.8适用)


    //移动剑到箭靶,并将剑放入箭靶节点跟随箭靶旋转
    jianMove() {
        if (!this.jian) {
            oops.log.trace('没有jian节点');
            return;
        }
        let position = this.jian.getPosition();
        position.y = 108; //todo::播放箭音

        let _jian = this.jian;
        tween(this.jian).to(0.1, {
            position: position
        }).call(() => {
            //放入箭靶节点,并更新坐标值
            let worldPosition = _jian.getWorldPosition();
            let copyJian = instantiate(this.jian);
            if (copyJian) {
                copyJian.setParent(this.jianTargetNode);
                copyJian.setWorldPosition(worldPosition); //旋转角度正常与当前箭靶的角度相反
                copyJian.angle = -this.jianBa.angle; //清空当前的剑变量

                //主动碰撞检测,看是否碰撞到其它箭体目标
                const col = copyJian.getComponent(PolygonCollider2D);
                if (!col) {
                    oops.log.logBusiness("箭体没有添加碰撞组件");
                } else {
                    // 放到下一帧再做查询,等待物理系统刷新
                    this.scheduleOnce(() => {
                        // 强制刷新碰撞器包围盒
                        col.apply();
                        const results = PhysicsSystem2D.instance.testAABB(col.worldAABB);
                        console.log("碰撞查询结果:", results);
                        // 过滤掉自己,判断是否命中其他箭
                        let isHitOtherArrow = false;
                        for (const c of results) {
                            if(c.node !== copyJian){
                                isHitOtherArrow = true;
                                break;
                            }
                        }
                        if(isHitOtherArrow){
                            oops.log.logBusiness("发生碰撞!");
                            // 碰撞业务逻辑写这里
                        } else {

            this.current++;


            this.updateTips();
            this.checkWin();
}
                    },0);
                }
            }

            _jian.destroy();
            this.jian = null; //再复制一把剑

            this.copyJianNode(); //碰撞检测后执行+1

        }).start();
    }

标签: none

添加新评论