如何用CocosCreator實現(xiàn)射擊小游戲
分析下制作步驟:
1. 準備好資源,搭建場景
資源的話可以自己到網(wǎng)上找,也可以直接用我的也行;創(chuàng)建好相應(yīng)文件夾,把資源放到res文件夾下;
搭建場景:
第一步:創(chuàng)建一個單色精靈(Script) bg 背景, 設(shè)置好顏色,加一個Widget組件,使其充滿屏幕;
第二步: 在bg
節(jié)點下創(chuàng)建top
和button
空節(jié)點作為頂與底部,然后在兩個空節(jié)點加入帶刺的節(jié)點(直接將圖片拖到top層級管理器就可以),現(xiàn)在我們需要給top與button
節(jié)點添加一個Layout組件
,屬性設(shè)置如圖,這樣可以看到屏幕上下都有刺了。
第三步: 將玩家小人、子彈、敵機同樣的方法加入到場景中,再創(chuàng)建一個Label節(jié)點用來顯示分數(shù),調(diào)節(jié)一下位置;
2. 代碼控制游戲
第一步: 創(chuàng)建一個game
腳本,掛載到dg
節(jié)點上;
第二步: 編輯代碼,在 properties
添加屬性,用來關(guān)聯(lián)玩家、子彈、敵人節(jié)點,再編輯器關(guān)聯(lián);
第三步: 代碼邏輯控制,包括初始化玩家、子彈、敵人;注冊監(jiān)聽事件;寫動作函數(shù);計分判斷等;
全部代碼:
cc.Class({ extends: cc.Component, properties: { playerNode: cc.Node, enemyNode: cc.Node, fireNode: cc.Node, scoreNode: cc.Label, }, onLoad () { this.playLoad(); this.fireLoad(); this.enemyLoad(); this.node.on("touchstart",this.fire,this); }, update (dt) { if(Math.abs(this.fireNode.y-this.enemyNode.y)<(this.fireNode.height/3+this.enemyNode.height/3) &&Math.abs(this.fireNode.x-this.enemyNode.x)<(this.fireNode.width/3+this.enemyNode.width/3)){ console.log("擊敗敵機"); this.scoreNode.string= ++this.score;//擊中得分 this.fireNode.stopAction(this.fireAction); this.enemyNode.stopAction(this.enemyAction); this.enemyNode.active=false; this.fireNode.active=false; this.fireLoad();//初始化子彈 this.enemyLoad();// 初始化敵機 } }, // 關(guān)閉事件監(jiān)聽 onDestroy(){ this.node.off("touchstart",this.fire,this); }, // 初始玩家 playLoad(){ this.score=0; this.playerNode.y=-cc.winSize.height/4; }, //初始化子彈 fireLoad(){ this.fireNode.active=true; this.isFire=false; this.fireNode.x=this.playerNode.x; this.fireNode.y=this.playerNode.y+this.playerNode.height; }, // 初始化敵機 enemyLoad(){ this.enemyNode.active=true; this.enemyNode.x=Math.random()* cc.winSize.width; this.enemyNode.y=cc.winSize.height/3; let x=cc.winSize.width/2-this.enemyNode.width/2; let y=Math.random()* cc.winSize.height/4; let seq=cc.repeatForever(cc.sequence(cc.moveTo(1.5,cc.v2(-x,y)),cc.moveTo(1.5,cc.v2(x,y)))); this.enemyAction=this.enemyNode.runAction(seq); }, // 死亡 重新加載游戲 dear(){ console.log("死亡"); cc.director.loadScene("game_scenes"); }, // 發(fā)射子彈 fire(){ if(this.isFire) return; this.isFire=true; console.log("開始發(fā)射"); var fireaction=cc.sequence( cc.moveTo(1,cc.v2(this.playerNode.x,cc.winSize.height/2)), cc.callFunc(()=>{ this.dear(); })); this.fireAction=this.fireNode.runAction(fireaction); console.log("結(jié)束發(fā)射"); } });
最終效果
以上就是如何用CocosCreator實現(xiàn)射擊小游戲的詳細內(nèi)容,更多關(guān)于CocosCreator實現(xiàn)射擊小游戲的資料請關(guān)注腳本之家其它相關(guān)文章!
- 詳解cocoscreater預(yù)制體prefab
- 如何在CocosCreator中利用常駐節(jié)點做圖層管理
- 游戲開發(fā)中如何使用CocosCreator進行音效處理
- CocosCreator ScrollView優(yōu)化系列之分幀加載
- 詳解CocosCreator項目結(jié)構(gòu)機制
- 如何使用CocosCreator對象池
- CocosCreator如何實現(xiàn)劃過的位置顯示紋理
- 整理CocosCreator常用知識點
- 全面講解CocosCreator熱更新
- CocosCreator經(jīng)典入門項目之flappybird
- CocosCreator通用框架設(shè)計之網(wǎng)絡(luò)
- 怎樣在CocosCreator中使用游戲手柄
相關(guān)文章
JavaScript中this的9種應(yīng)用場景及三種復(fù)合應(yīng)用場景
本文通過9中應(yīng)用場景給大家分析this關(guān)鍵字的用法,需要了解的朋友可以參考下本文2015-09-09JavaScript實現(xiàn)QQ列表展開收縮擴展功能
這篇文章主要為大家詳細介紹了JavaScript實現(xiàn)QQ列表展開收縮擴展功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10JavaScript數(shù)學(xué)對象之數(shù)字進制轉(zhuǎn)換
這篇文章主要為大家講解了JavaScript數(shù)學(xué)對象——數(shù)字進制轉(zhuǎn)換的實現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-05-05