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游戏开发(第一篇)

发布了39 篇原创文章 · 获赞 48 · 访问量 9225

猜你喜欢

转载自blog.csdn.net/qq_45021180/article/details/104400728