有两种合成大西瓜的思路

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}},

cocos 合成大西瓜思路相关推荐

  1. 保姆式Cocos合成大西瓜案例

    漫天的冰雪冻结了我的心,但它冻结不了我心中的正义,我愿用我严寒的身躯冰冻这世间的邪恶. 往期文章分享 养不起真猫,就用代码吸猫-Unity粒子实现画猫咪 点击跳转=>熬夜再战Android从青铜 ...

  2. Cocos合成大西瓜案例-下

    我披上坚甲,挡在你身前,万千的关心只化为一句:小心. 往期文章分享 点击跳转=>熬夜再战Android从青铜到王者-开发效率插件篇 点击跳转=>Unity粒子特效系列-龙卷风预制体做好了, ...

  3. cocos creator |《合成大西瓜》源码 解读

    更多源码请扫码关注公众号 编者荐语: 不辜负每一份热爱 以下文章来源于懒惰的An ,作者懒惰的An 懒惰的An <合成大西瓜>原版开发者,cocos游戏开发小辣鸡,会发一些自己做的游戏源码 ...

  4. 合成大西瓜(西瓜雨版)及改版思路(保姆式教程)

    合成大西瓜(西瓜雨版)及改版思路(保姆式教程) 最近一款名为"合成大西瓜"的小游戏突然火了,上了几次微博热搜,身边有些人还在感慨:瓜都吃不完了,还叫我合成瓜- 自己玩了也觉得很有意 ...

  5. 从0开始实现一个合成大西瓜

    作者:橙红年代 (https://juejin.cn/post/6923803717808422925) 最近微博上曝出了很多瓜,"合成大西瓜"这个游戏也很火热,玩了一阵还挺有意思 ...

  6. 撸了一个「合成大西瓜」

    最近「合成大西瓜」这个游戏也很火热,玩了一阵还挺有意思的.研究了一下原理,发现目前流传的版本都是魔改编译后的版本,代码经过压缩不具备可读性,因此决定自己照着实现一个.这个游戏已经开源,获取项目可以关注 ...

  7. 前端项目:从0开始实现一个合成大西瓜

    最近微博上曝出了很多瓜,"合成大西瓜"这个游戏也很火热,玩了一阵还挺有意思的.研究了一下原理,发现目前流传的版本都是魔改编译后的版本,代码经过压缩不具备可读性,因此决定自己照着实现 ...

  8. 为了女朋友!熬夜撸了一个“合成大西瓜”!(附源码)

    本项目主要用作 cocos creator 练手使用,所有美术素材和音频材料均来源于 游戏逻辑 整个游戏逻辑比较简单,结合了俄罗斯方块与消除游戏的核心玩法 在生成一个水果 点击屏幕,水果移动到对应x轴 ...

  9. 魔改和上线你的合成大西瓜,最全教程!

    本文是从 0 到 1 的教程,让小白也能够魔改和上线发布属于你的合成大西瓜! 最近,一款名为『 合成大西瓜 』的游戏突然火了!看来真的是大家吃瓜吃太多了,这个小游戏深抓人心! 当然,游戏本身非常有趣, ...

最新文章

  1. iOS 5解决Could not instantiate class named NSLayoutConstraint问题
  2. 【转】HashMap集合中key只能为引用数据类型,不能为基本类型
  3. Java Servlet和JSP教程
  4. 阿里启动NASA计划创造新经济核心科技
  5. 大学计算机要学多久,大学刚开学要不要带电脑?很多人都很后悔,学长学姐把经验告诉你...
  6. 程序员经典面试题,高并发系统,一般需要怎么做
  7. 面试题01.02 判定是否互为字符重排
  8. poj Ancient Cipher 古代密码
  9. 《算法图解》---笔记
  10. js ajax上传file文件上传,使用ajaxfileupload.js实现上传文件功能
  11. 实现一个简单的类似spring的pointcut正则表达式
  12. cad快速选择命令快捷键_CAD人必知的6大CAD操作命令及快捷键
  13. git 报错:unable to access “http://gitlab.tydic.com:7666/tx-preformance/tx……“解决方法
  14. SHELL的文本处理工具
  15. 创业日志:一个和尚挑水喝,两个和尚抬水喝,三个和尚没水喝?
  16. 专家访谈:Flex技术对web开发的影响
  17. 成都市中小学计算机创客,我校荣获2019四川省中小学电脑制作活动机器人暨创客竞赛团体一等奖...
  18. linux挂移动硬盘命令,linux挂载命令mount及U盘、移动硬盘的挂载
  19. window XP驱动开发(一)如何下载WDK
  20. uva1593代码对齐

热门文章

  1. 详述 MIMIC 数据库 26张数据表(二)之 五种字典表
  2. OpenCV 录制视频
  3. Android 模拟手指滑动
  4. Unity图集优化原理
  5. 全球及中国移动多媒体市场状况分析与运营模式咨询报告2022版
  6. #Linux基础(三)
  7. 中兴OLT ZXA10 C200 V1.1.3P2T6固件
  8. 读论文《A Neural Probabilistic Language Model》
  9. 【图】【热传】安全漏洞破解的奇迹
  10. autoCAD 创建对象 使用面域 创建图案填充