cocos 合成大西瓜思路

有两种合成大西瓜的思路

1.在合成的时候利用碰撞回调函数直接关闭对方的逻辑,只执行其一的逻辑(这种有可能会出问题,但是js没有线程问题,网上是这么解释的因为js运行在浏览器上,在同一时间只能做一件事所以不会出现线程问题)

在发生碰撞回调的时候,先执行碰撞回调函数的一方会把对方的碰撞体积设置为0,就不会执行相同的逻辑,只有一方能执行合成逻辑。

    onBeginContact(contact, selfCollider, otherCollider) {

        if (otherCollider.node.group == "fruit") {
            //this.endCtrl = true;
            //  只有下方的水果触发碰撞回调(次要的逻辑)
            if (selfCollider.node.y < otherCollider.node.y) {
                return
            }
            //  水果一下落,放在FruitNode节点下
            selfCollider.node.parent = cc.find("Canvas/FruitNode");
            if (selfCollider.node.getComponent(cc.RigidBody) != null) {
                selfCollider.node.getComponent(cc.RigidBody).angularVelocity = 0;
                // 限制一下线速度
            }
            let selfNum = this.fruitNumber;
            let otherNum = otherCollider.node.getComponent("Fruit").fruitNumber;
            // cc.log(selfNum,otherNum);
            //  水果类型相同的合成
            if (selfNum == otherNum && selfNum < 9 && otherNum < 9) {
                if (selfCollider.node.getComponent("Fruit").getNumber() == 0) {
                    otherCollider.node.getComponent(cc.PhysicsCircleCollider).radius = 0;
                    otherCollider.node.getComponent(cc.PhysicsCircleCollider).apply()
                    this.node.getComponent(cc.PhysicsCircleCollider).radius = 0;
                    this.node.getComponent(cc.PhysicsCircleCollider).apply();
                    cc.tween(selfCollider.node)
                        .to(0.1, { position: otherCollider.node.position, scale: 0 })
                        .call(() => {
                            //生成下一个等级的水果
                            this.game.score += this.fruitNumber + 1;
                            this.game.fruitLevelUp(this.fruitNumber + 1, otherCollider.node.position);
                            otherCollider.node.active = false;
                            selfCollider.node.active = false;
                            otherCollider.node.destroy();
                            selfCollider.node.destroy();
                        })
                        .start()
                }
            } else if (selfNum == otherNum && selfNum == 9 && otherNum == 9) {
                if (selfCollider.node.getComponent("Fruit").getNumber() == 0) {
                    otherCollider.node.getComponent(cc.PhysicsCircleCollider).radius = 0;
                    otherCollider.node.getComponent(cc.PhysicsCircleCollider).apply()
                    this.node.getComponent(cc.PhysicsCircleCollider).radius = 0;
                    this.node.getComponent(cc.PhysicsCircleCollider).apply();
                    cc.tween(selfCollider.node)
                        .to(0.1, { position: otherCollider.node.position })
                        .call(() => {
                            this.game.score += this.fruitNumber + 1;
                            this.game.fruitLevelUp(this.fruitNumber + 1, otherCollider.node.position);
                            // otherCollider.node.active = false;
                            // selfCollider.node.active = false;
                            otherCollider.node.destroy();
                            selfCollider.node.destroy();
                        })
                        .start()

                }
            }
        }

2.考虑到进程可能会发生冲突,比如陷入双方都把对方的逻辑关闭的窘境所以要实现互斥锁的并发程序有两种解决办法,一种就是皮特森算法,这里不多做介绍了,还有一种就是利用整体数据的修改来判断进入的逻辑:(一下子不知道如何说明白直接上代码)

    onBeginContact: function (contact, selfCollider, otherCollider) {
        if (otherCollider.tag === selfCollider.tag) {
            cc.find("Canvas/gameLayer").getComponent("Game").pos = cc.v2((contact.colliderA.node.position.x + contact.colliderB.node.position.x) / 2, (contact.colliderA.node.position.y + contact.colliderB.node.position.y) / 2);
            this.node.removeComponent(cc.PhysicsCircleCollider);
            this.node.removeComponent(cc.RigidBody);
            // 加分
            this.addScore();
            // 播放消失动画
            this.node.getChildByName("blast").active = true;
            // 事件发射给父节点接收 告知出现合成
            this.node.dispatchEvent(new cc.Event.EventCustom("add", true));
            // 销毁节点
            this.scheduleOnce(() => {
                this.node.destroy();
            }, 0.2);
            if(otherCollider.tag === 10 || otherCollider.tag === 100){
                cc.find("Canvas/overLayer").active = true;
            }
        }
    },

因为相同tag的水果会合成,在总分上会加上相同的分数,并且他们两个的接触的时候中心位置都是一样的,所以利用这些共有的条件来判断生成水果类型和水果位置。

在主脚本的控制合成逻辑(根据合成的总分来判断生成的是什么类型的水果)

    /******************************合成事件*************************** */
    calculate(){
        this.addTimes += 1;
        // cc.log(this.addTimes)
        if(this.addTimes === 2){
            this.addTimes = 0;
            // 通过合成点位置生成新的水果 并初始化
            this.createNew(this.pos)
            this.pos = cc.v2(0,0);
            // 更新分数
            gameData.score += gameData.curAddition;
            gameData.curAddition = 0;
            this.scoreTxt.string = gameData.score;
            // this.scheduleOnce(()=>{gameData.curAddition = 0},0.2);
        }
        
    },

    createNew(loc){
        // 确定生成的水果编号
        let index = this.choose();
        //初始化生成位置
        let fruit = cc.instantiate(gameData.storeHouse["prefab"]["fruit"]);
        fruit.position = cc.v2(loc);
        this.node.addChild(fruit);
        // 设置宽高
        fruit.width = gameData.fruitData["q_00" + index].size;
        fruit.height = gameData.fruitData["q_00" + index].size;
        // 设置碰撞半径
        fruit.getComponent(cc.PhysicsCircleCollider).radius = gameData.fruitData["q_00" + index].radius;
        fruit.getComponent(cc.RigidBody).linearVelocity = cc.v2(0,-100);
        fruit.getComponent(cc.RigidBody).gravityScale = 1;
        fruit.getComponent("Fruit").init(index);
    },

    choose(){
        if(gameData.curAddition / 2 === 1){
            return 2;
        }
        else if(gameData.curAddition / 2 === 2){
            return 3;
        }
        else if(gameData.curAddition / 2 === 4){
            return 4;
        }
        else if(gameData.curAddition / 2 === 8){
            return 5;
        }
        else if(gameData.curAddition / 2 === 16){
            return 6;
        }
        else if(gameData.curAddition / 2 === 32){
            return 7;
        }
        else if(gameData.curAddition / 2 === 64){
            return 8;
        }
        else if(gameData.curAddition / 2 === 128){
            return 9;
        }
        else if(gameData.curAddition / 2 === 256){
            return 10;
        }
        else if(gameData.curAddition / 2 === 512){
            return 11;
        }else{
            return 0
        }
    },

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: darkbzoj.cc是一个在线的题库和程序设计平台。与其他常见的程序设计平台不同的是,darkbzoj.cc的难度较高,所以它更适合经验更为丰富的用户。平台集成了各种不同难度的编程挑战题目,包括算法、数学、图形、字符串、数据结构等领域,满足不同技能层级的编程爱好者需求。 在darkbzoj.cc的题库中,有许多有趣的题目,涵盖了各种算法和数据结构,如并查集、线段树、动态规划等。此外,该平台还可以服务于程序设计比赛,推广和发展技能水平。 因此,darkbzoj.cc不仅是一个有趣的编程平台,而且也是学习和提高编程技能的理想场所,值得更多编程爱好者的尝试和探索。 ### 回答2: darkbzoj.cc是一个在线编程题库,也可以称为黑暗集训队题库。这个网站的题目大部分都是计算机竞赛的经典题目,难度较高,主要面向ACM/ICPC竞赛选手、OI/NOI选手、算法研究人员以及对算法挑战感兴趣的程序员们。 这个网站包含了很多题目,其中很多都是经典的算法题目,如最短路、最小生成树、网络流、计算几何、字符串算法等。对于那些对这些经典算法感兴趣的人来说,这个网站是一个很好的学习资源。 除此之外,这个网站还提供了一些更加高级的算法题目,需要一些高级的知识来解决。因此,对于对于那些挑战自己的人来说,这个网站也是一个很好的选择。 总之,darkbzoj.cc提供了一个充满挑战的编程题库,为计算机科学专业的学生们以及程序员们提供了一个测试和提高他们编程技能的地方。 ### 回答3: Darkbzoj.cc是一个在线的算法竞赛平台,旨在为全球的程序员提供优秀的学习资源和挑战比赛。通过Darkbzoj.cc,用户可以学习算法、优化程序、提高编程技巧,并在比赛中体验竞技的乐趣。 Darkbzoj.cc提供了丰富的学习资料和编程题目,包括数据结构、算法基础、动态规划、图论以及数论等方面的知识。该平台具有良好的用户体验,界面清晰简洁,让用户更加专注于学习和编程。 此外,Darkbzoj.cc还提供了许多高质量的比赛,并与全球知名的算法竞赛平台进行合作,包括Codeforces、Atcoder、Topcoder等。这些比赛不仅能够提供良好的竞赛体验,还能够激励用户学习和提高自己的编程技能。 总之,Darkbzoj.cc是一个优秀的算法竞赛平台,不仅提供了优质的学习资源和编程题目,还能够为用户提供竞赛切磋的机会,是全球程序员必备的学习和竞赛平台。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

永远的毅嘉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值