aircraft-war(三)
上一章的內(nèi)容務(wù)必搞懂再進(jìn)入這一步!
這部分先來實(shí)現(xiàn)一個(gè)簡(jiǎn)單的功能,Hero接觸道具然后換成雙彈道子彈。
涉及的知識(shí)點(diǎn)是:物理系統(tǒng)——碰撞
首先,給Hero添加一個(gè)多邊形碰撞組件,勾選Editing進(jìn)入編輯狀態(tài):
(這一步需要給Hero添加一個(gè)檢測(cè)碰撞的區(qū)域)

image.png
編輯完成之后,記得把Editing勾掉。
然后在hero腳本中添加:
cc.Class({
extends: cc.Component,
properties: {
},
// use this for initialization
onLoad: function () {
// 監(jiān)聽拖動(dòng)事件
this.node.on('touchmove', this.onHandleHeroMove, this);
// 獲取碰撞檢測(cè)系統(tǒng)
let manager = cc.director.getCollisionManager();
// 開啟碰撞檢測(cè)系統(tǒng)
manager.enabled = true;
},
// Hero拖動(dòng)
onHandleHeroMove: function (event) {
// touchmove事件中 event.getLocation() 獲取當(dāng)前已左下角為錨點(diǎn)的觸點(diǎn)位置(world point)
let position = event.getLocation();
// 實(shí)際hero是background的子元素,所以坐標(biāo)應(yīng)該是隨自己的父元素進(jìn)行的,所以要將“world point”轉(zhuǎn)化為“node point”
let location = this.node.parent.convertToNodeSpaceAR(position);
this.node.setPosition(location);
},
// 碰撞組件
onCollisionEnter: function (other, self) {
console.log(other.world);
}
// called every frame, uncomment this function to activate update callback
// update: function (dt) {
// },
});
這個(gè)時(shí)候可以看到,碰撞檢測(cè)已經(jīng)開啟了。下面繼續(xù)完成剩下的部分,碰觸道具-更換雙彈道子彈-時(shí)間超過切換回單彈道子彈:
首先需要編輯:
// bulletGroup.js
// 有限時(shí)長(zhǎng)子彈
const finiteBullet = cc.Class({
extends: infiniteBullet,
name: 'finiteBullet',
properties: {
duration: 0,
ufoBulletName: ''
}
});
// ...
// 更換子彈
changeBullet: function (ufoBulletName) {
this.unschedule(this.startShoot);
for (let i = 0; i < this.finiteBullet.length; i++) {
if (this.finiteBullet[i].ufoBulletName === ufoBulletName) {
let startDoubleShoot = function (i) {
this.genNewBullet(this.finiteBullet[i])
}.bind(this, i);
// 設(shè)置一個(gè)延時(shí),當(dāng)一個(gè)定時(shí)器走完之后,另一個(gè)延時(shí)結(jié)束,開始執(zhí)行
this.schedule(startDoubleShoot, this.finiteBullet[i].rate, this.finiteBullet[i].duration);
let delay = this.finiteBullet[i].rate * this.finiteBullet[i].duration;
this.schedule(this.startShoot, this.infiniteBullet.rate, cc.macro.REPEAT_FOREVER, delay);
}
}
},
然后是hero腳本:
cc.Class({
extends: cc.Component,
properties: {
bulletGroup: {
default: null,
type: require('bulletGroup'),
}
},
// ...
// 碰撞組件
onCollisionEnter: function (other, self) {
if (other.node.name === 'doubleBullet') {
this.bulletGroup.changeBullet(other.node.name);
}
}
});
bulletGroup:

image.png
hero:

image.png
一直到這里,子彈部分算是功能都完成了,音效的部分,準(zhǔn)備放在最后統(tǒng)一加,因?yàn)橛X得音效與業(yè)務(wù)的相關(guān)性是最小的而且實(shí)現(xiàn)是最簡(jiǎn)單的。代碼在這里
接下來準(zhǔn)備開始制作敵人。敵人的運(yùn)動(dòng)軌跡是自頂上下在Y軸移動(dòng),一共有三種類型的敵機(jī),數(shù)量也不相同,防御力也不同,而且受到攻擊時(shí),要考慮掉血量。敵機(jī)被擊落,會(huì)有爆炸效果,Hero被擊落,也同樣有爆炸效果。