cocos 合成大西瓜思路
生活随笔
收集整理的這篇文章主要介紹了
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}},總結
以上是生活随笔為你收集整理的cocos 合成大西瓜思路的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: github+hexo+butterfl
- 下一篇: bootstrap框架过时了吗_浅议bo