游戲開發中,碰撞檢測無處不在,今天就通過一個簡單的小游戲教你學會如何在 Cocos Creator 中進行碰撞檢測。配合官方文檔學習效果更加(官方文檔傳送門:https://docs.cocos.com/creator/manual/zh/physics/collision/),關注公眾號「游戲開發小白變怪獸」后臺回復「解救人質」獲取美術資源及源碼。
游戲玩法:
通過控制手槍位置,松手發射子彈擊中躲在人質后面的歹徒順利解救人質,小心不要打中人質哦!
實現邏輯:
分別給子彈、人質和歹徒添加碰撞組件,檢測到子彈與歹徒發生碰撞時,營救成功;檢測到子彈與人質發生碰撞時,營救失敗。
步驟詳解:
1.按照圖中節點樹創建節點,分別將對應貼圖拖給對應的節點,并設置節點位置如圖,successLabel 和 failLabel 內容分別為「解救成功!」和「解救失??!」:
2.給 hostage 節點添加碰撞組件,并設置組件 Tag 屬性和 Size 屬性:
當一個節點上有多個碰撞組件時,在發生碰撞后,可以使用 Tag 來判斷是節點上的哪個碰撞組件被碰撞了。此時,碰撞組件大小和節點大小一致,同樣的步驟將 enemy 和 bullet 節點添加好碰撞組件。
3.接下來在項目設置面板里添加分組:hostage、enemy 和 bullet(注:分組添加后是不可以刪除的,不過你可以任意修改分組的名字),并勾選hostage 和 bullet、enemy 和 bullet:
4.在項目設置添加好分組后,分別在 hostage、enemy 和 bullet 屬性中的 Group 設置對應分組:
,【的身】【像是】【知道】【驚不】【從今】【族的】【話那】【不能】【從的】【數不】【了血】【密沒】【黑色】【口大】【了解】黑帽seo技術【界開】【任何】【號沒】【法時】【蟲神】【四重】【大乍】【一抽】【增長】【尊水】【靈樹】【地還】,
5.接下來新建 Bullet.js 腳本掛載到 bullet 節點下,編輯腳本如下,主要在 update 方法內實現了子彈的移動和銷毀,以及碰撞回調函數(注:使用碰撞檢測之前一定要獲取碰撞檢測,且碰撞回調函數名稱固定,無需注冊?。?/p>
1 // Bullet.js 2 3 cc.Class({ 4 extends: cc.Component, 5 6 properties: { 7 mSpeed: 300, 8 }, 9 10 // LIFE-CYCLE CALLBACKS: 11 12 // onLoad () {}, 13 14 start() { 15 var manager = cc.director.getCollisionManager(); // 獲取碰撞檢測系統 16 manager.enabled = true; 17 }, 18 19 update(dt) { // 設置子彈移動,當超出屏幕范圍未發生碰撞時自動銷毀 20 this.node.y += this.mSpeed * dt; 21 22 if (this.node.y > 580) { 23 console.log('超出屏幕范圍,子彈銷毀!'); 24 25 this.node.destroy(); 26 } 27 }, 28 29 /** 30 * 當碰撞產生的時候調用 31 * @param {Collider} other 產生碰撞的另一個碰撞組件 32 * @param {Collider} self 產生碰撞的自身的碰撞組件 33 */ 34 onCollisionEnter: function (other, self) { 35 console.log('on collision enter'); 36 37 if (other.tag == 1) { // 子彈碰到人質時,解救失??! 38 console.log('解救人質失?。?); 39 40 var failLabel = this.node.parent.getChildByName('failLabel'); 41 failLabel.active = true; 42 43 this.node.destroy(); 44 45 } else if (other.tag == 2) { // 子彈碰到敵人時,解救成功! 46 console.log('解救人質成功!'); 47 48 var successLabel = this.node.parent.getChildByName('successLabel'); 49 successLabel.active = true; 50 51 this.node.destroy(); 52 } 53 }, 54 55 /** 56 * 當碰撞產生后,碰撞結束前的情況下,每次計算碰撞結果后調用 57 * @param {Collider} other 產生碰撞的另一個碰撞組件 58 * @param {Collider} self 產生碰撞的自身的碰撞組件 59 */ 60 onCollisionStay: function (other, self) { 61 console.log('on collision stay'); 62 }, 63 64 /** 65 * 當碰撞結束后調用 66 * @param {Collider} other 產生碰撞的另一個碰撞組件 67 * @param {Collider} self 產生碰撞的自身的碰撞組件 68 */ 69 onCollisionExit: function (other, self) { 70 console.log('on collision exit'); 71 } 72 });
編寫完腳本后,將 bullet 節點保存為預制件待用。
6.然后編寫 gun 節點的控制邏輯腳本 ControlGun.js:
1 // ControlGun.js 2 3 cc.Class({ 4 extends: cc.Component, 5 6 properties: { 7 mBullet: cc.Prefab 8 }, 9 10 // LIFE-CYCLE CALLBACKS: 11 12 onLoad() { }, 13 14 start() { 15 this.node.on('touchstart', this.onTouchStart, this); 16 this.node.on('touchmove', this.onTouchMove, this); 17 this.node.on('touchend', this.ontouchEnd, this); 18 }, 19 20 // update (dt) {}, 21 22 onTouchStart(event) { // 每次點擊之前,都要把結果關掉 23 var successLabel = this.node.parent.getChildByName('successLabel'); 24 successLabel.active = false; 25 26 var failLabel = this.node.parent.getChildByName('failLabel'); 27 failLabel.active = false; 28 }, 29 30 onTouchMove(event) { // 控制節點在屏幕范圍內左右移動 31 let rangePos = event.getDelta(); 32 33 this.node.x += rangePos.x; 34 35 let minX = -this.node.parent.width / 2 + this.node.width / 2; 36 let maxX = Math.abs(minX); 37 38 let currentPos = this.node.getPosition(); 39 40 if (currentPos.x < minX) { 41 currentPos.x = minX; 42 } else if (currentPos.x > maxX) { 43 currentPos.x = maxX; 44 } 45 46 this.node.setPosition(currentPos); 47 }, 48 49 ontouchEnd(event) { // 松開時發射子彈 50 console.log('發射子彈'); 51 52 let bullet = cc.instantiate(this.mBullet); 53 bullet.parent = this.node.parent; 54 55 let currentPos = this.node.getPosition(); 56 57 bullet.parent = this.node.parent; 58 bullet.setPosition(currentPos); 59 } 60 });
7.最后編寫 enemy 的移動腳本:
1 // ControlEnemy.js 2 3 cc.Class({ 4 extends: cc.Component, 5 6 properties: { 7 mSpeed: 300 8 }, 9 10 // LIFE-CYCLE CALLBACKS: 11 12 // onLoad () {}, 13 14 start() { 15 this.minX = -this.node.parent.width / 2 + this.node.width / 2; 16 this.maxX = Math.abs(this.minX); 17 }, 18 19 update(dt) { 20 let currentPos = this.node.getPosition(); 21 22 if (currentPos.x < this.minX) { 23 this.mSpeed = Math.abs(this.mSpeed); 24 } else if (currentPos.x > this.maxX) { 25 this.mSpeed = -Math.abs(this.mSpeed); 26 } 27 28 this.node.x += this.mSpeed * dt; 29 } 30 });
8.編寫完所有的腳本之后,就可以預覽游戲了,快來試試你能不能成功的就下人質吧!
最后:
通過這個簡單的小游戲有沒有學會碰撞檢測的使用呢?快來下載美術資源嘗試一下吧!關注公眾號「游戲開發小白變怪獸」后臺回復「解救人質」獲取美術資源及源碼,更有更多優質內容等你發現!
推薦閱讀:
一文教你實現「飛機大戰」里戰機的控制邏輯
自定義虛擬搖桿組件讓你一勞永逸
Cocos Creator 如何進行斷點調試?
如何部署 H5 游戲到云服務器?
我是「Super于」,立志做一個每天都有正反饋的人!
|轉載請注明來源地址:蜘蛛池出租 http://m.gzxyxkj.cn/
專注于SEO培訓,快速排名黑帽SEO https://www.heimao.wiki