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