蜘蛛池出租蜘蛛池出租

蜘蛛池網(wǎng)站收錄技術(shù)

貴州博彩詞黑帽seo:通過「解救人質(zhì)」小游戲教你學(xué)會碰撞檢測_黑帽SEO培訓(xùn)

:【面試】足夠“忽悠”面試官的『Spring事務(wù)管理器』源碼閱讀梳理(建議珍藏)

游戲開發(fā)中,碰撞檢測無處不在,今天就通過一個簡單的小游戲教你學(xué)會如何在 Cocos Creator 中進行碰撞檢測。配合官方文檔學(xué)習(xí)效果更加(官方文檔傳送門:https://docs.cocos.com/creator/manual/zh/physics/collision/),關(guān)注公眾號「游戲開發(fā)小白變怪獸」后臺回復(fù)「解救人質(zhì)」獲取美術(shù)資源及源碼。

 

游戲玩法:

通過控制手槍位置,松手發(fā)射子彈擊中躲在人質(zhì)后面的歹徒順利解救人質(zhì),小心不要打中人質(zhì)哦!

 

實現(xiàn)邏輯:

分別給子彈、人質(zhì)和歹徒添加碰撞組件,檢測到子彈與歹徒發(fā)生碰撞時,營救成功;檢測到子彈與人質(zhì)發(fā)生碰撞時,營救失敗。

 

步驟詳解:

1.按照圖中節(jié)點樹創(chuàng)建節(jié)點,分別將對應(yīng)貼圖拖給對應(yīng)的節(jié)點,并設(shè)置節(jié)點位置如圖,successLabel 和 failLabel 內(nèi)容分別為「解救成功!」和「解救失敗!」:

 

2.給 hostage 節(jié)點添加碰撞組件,并設(shè)置組件 Tag 屬性和 Size 屬性:

 

當(dāng)一個節(jié)點上有多個碰撞組件時,在發(fā)生碰撞后,可以使用 Tag 來判斷是節(jié)點上的哪個碰撞組件被碰撞了。此時,碰撞組件大小和節(jié)點大小一致,同樣的步驟將 enemy 和 bullet 節(jié)點添加好碰撞組件。

 

3.接下來在項目設(shè)置面板里添加分組:hostage、enemy 和 bullet(注:分組添加后是不可以刪除的,不過你可以任意修改分組的名字),并勾選hostage 和 bullet、enemy 和 bullet:

 

4.在項目設(shè)置添加好分組后,分別在 hostage、enemy 和 bullet 屬性中的 Group 設(shè)置對應(yīng)分組:

,【的身】【像是】【知道】【驚不】【從今】【族的】【話那】【不能】【從的】【數(shù)不】【了血】【密沒】【黑色】【口大】【了解】黑帽seo技術(shù)【界開】【任何】【號沒】【法時】【蟲神】【四重】【大乍】【一抽】【增長】【尊水】【靈樹】【地還】,

 

5.接下來新建 Bullet.js 腳本掛載到 bullet 節(jié)點下,編輯腳本如下,主要在 update 方法內(nèi)實現(xiàn)了子彈的移動和銷毀,以及碰撞回調(diào)函數(shù)(注:使用碰撞檢測之前一定要獲取碰撞檢測,且碰撞回調(diào)函數(shù)名稱固定,無需注冊?。?/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();    // 獲取碰撞檢測系統(tǒng)
16         manager.enabled = true;
17     },
18 
19     update(dt) {    // 設(shè)置子彈移動,當(dāng)超出屏幕范圍未發(fā)生碰撞時自動銷毀
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     * 當(dāng)碰撞產(chǎn)生的時候調(diào)用
31     * @param  {Collider} other 產(chǎn)生碰撞的另一個碰撞組件
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ì)時,解救失?。?/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) {    // 子彈碰到敵人時,解救成功!
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é)束前的情況下,每次計算碰撞結(jié)果后調(diào)用
57     * @param  {Collider} other 產(chǎn)生碰撞的另一個碰撞組件
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)生碰撞的另一個碰撞組件
67     * @param  {Collider} self  產(chǎn)生碰撞的自身的碰撞組件
68     */
69     onCollisionExit: function (other, self) {
70         console.log('on collision exit');
71     }
72 });

 

編寫完腳本后,將 bullet 節(jié)點保存為預(yù)制件待用。

 

6.然后編寫 gun 節(jié)點的控制邏輯腳本 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) {    // 每次點擊之前,都要把結(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é)點在屏幕范圍內(nèi)左右移動
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) {    // 松開時發(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.最后編寫 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.編寫完所有的腳本之后,就可以預(yù)覽游戲了,快來試試你能不能成功的就下人質(zhì)吧!

 

最后:

通過這個簡單的小游戲有沒有學(xué)會碰撞檢測的使用呢?快來下載美術(shù)資源嘗試一下吧!關(guān)注公眾號「游戲開發(fā)小白變怪獸」后臺回復(fù)「解救人質(zhì)」獲取美術(shù)資源及源碼,更有更多優(yōu)質(zhì)內(nèi)容等你發(fā)現(xiàn)!

 

 

推薦閱讀:

 

一文教你實現(xiàn)「飛機大戰(zhàn)」里戰(zhàn)機的控制邏輯

自定義虛擬搖桿組件讓你一勞永逸

Cocos Creator 如何進行斷點調(diào)試?

如何部署 H5 游戲到云服務(wù)器?

 

 

我是「Super于」,立志做一個每天都有正反饋的人!

?

|轉(zhuǎn)載請注明來源地址:蜘蛛池出租 http://www.wholesalehouseflipping.com/
專注于SEO培訓(xùn),快速排名黑帽SEO https://www.heimao.wiki

版權(quán)聲明:本文為 “蜘蛛池出租” 原創(chuàng)文章,轉(zhuǎn)載請附上原文出處鏈接及本聲明;

原文鏈接:http://www.wholesalehouseflipping.com/post/17862.html

相關(guān)文章

?    2025年11月    ?
12
3456789
10111213141516
17181920212223
24252627282930

搜索

控制面板

您好,歡迎到訪網(wǎng)站!
  查看權(quán)限

網(wǎng)站分類

最新留言

標(biāo)簽列表

最近發(fā)表

作者列表

站點信息

  • 文章總數(shù):10559
  • 頁面總數(shù):3
  • 分類總數(shù):7
  • 標(biāo)簽總數(shù):40
  • 評論總數(shù):783
  • 瀏覽總數(shù):3557254

友情鏈接

免费国产亚洲天堂AV,国产又粗又猛又黄又爽视频,亚州国产精品一线北,国产线播放免费人成视频播放