CocosCreator 經典飛刀小遊戲 (實戰)

這是一款比較經典的小遊戲了,新手可以做些這種工程量比較小的遊戲來練練手!

項目下載鏈接:https://download.csdn.net/download/qq_45021180/12172205
在這裏插入圖片描述


有了前面的基礎,現在看一下項目界面和代碼就可以完全明白了~

項目界面介紹:
在這裏插入圖片描述

全部代碼:


cc.Class({
    extends: cc.Component,

    properties: {
        Wheel: cc.Node,  // 飛輪節點
        Knife: cc.Node,  // 飛刀節點
        PrefabKnife: cc.Prefab, // 飛刀預製體
        Garde: cc.Label,  // 分數組件
    },

     onLoad () {
         this.Wheel.zIndex=1;  //飛輪在最上面可以擋住飛刀
         this.Speed=3;// 旋轉速度
         this.garde=0;// 分數
         this.Throw=true;
         this.KnifeArray=[]; // 存放預製體數組
         setInterval(()=>{// 計時器,沒1秒鐘,改變一次速度和方向
             let dir=Math.random() < 0.5 ? 1 :-1;
             this.Speed=(1+Math.random()*4)*dir;
         },1000)
         this.node.on("touchstart",this.Knifefly,this);// 註冊觸摸事件
     },

     onDestroy(){
        this.node.off("touchstart",this.Knifefly,this);
     },
     
     //飛刀動作
    Knifefly(){
        if(this.Throw){
            this.Throw=false;
            var seq=cc.sequence(cc.moveTo(0.25,this.Wheel.x,this.Wheel.y-this.Wheel.height/2),
            cc.callFunc(()=>{
                let fg=false;// fg用來判斷飛刀是否與已經在飛輪上的飛刀相撞
                
                for(let kf of this.KnifeArray){
                    if(Math.abs(kf.angle-this.Knife.angle)<15){
                        fg=true;
                        break;
                    }
                }

                if(fg){ // 如果相撞,重新開始遊戲
                    cc.director.loadScene("game_scenes");
                }
                else{
                    this.garde+=1;
                    let kf=cc.instantiate(this.PrefabKnife); // 實例化一個飛刀
                    kf.setPosition(this.Knife.position); // 替換飛刀
                    this.node.addChild(kf); // 加入場景
                    this.KnifeArray.push(kf);
                    this.Knife.setPosition(0,-230); // 飛刀歸位
                    this.Throw=true;
                }
            })
        );
        this.Knife.runAction(seq);
        }
    },
     update (dt) {
         this.Garde.string=this.garde; // 更新分數
         this.Wheel.angle=(this.Wheel.angle+this.Speed)%360; //改變飛輪角度,使其旋轉
         for(let kf of this.KnifeArray){//使每一個飛刀都跟着飛輪旋轉起來
            kf.angle=(kf.angle+this.Speed)%360;
            let r=this.Wheel.height/2;
            let dar=(kf.angle-90)*Math.PI/180;
            kf.setPosition(this.Wheel.x+r*Math.cos(dar),this.Wheel.y+r*Math.sin(dar));
         }
     },
});

推薦閱讀:
一個小時完成CocosCreator射擊小遊戲 (適合初學者)
走進Cocos Creator遊戲開發(第一篇)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章