内容纲要

1.载入游戏资源,搭建场景
飞机大战插图
2.实现飞机移动和发射子弹
– 移动

        if(this.up)
         {
            if(this.node.y<305)
            this.node.y+=5;
         }
         if(this.down)
         {
             if(this.node.y>-305)
            this.node.y-=5;
         }
         if(this.left)
         {
             if(this.node.x>-187)
            this.node.x-=5;
         }
         if(this.right)
         {
             if(this.node.x<187)
            this.node.x+=5;
         }
  • 生成子弹
spawnPlayerBullet()
{
            if(this.type==1)
            {
                var pb1 = cc.instantiate(this.playerBulletPrefab);
                this.node.parent.addChild(pb1);
                pb1.setPosition(this.node.x-10,this.node.y+25);

                var pb2 = cc.instantiate(this.playerBulletPrefab);
                this.node.parent.addChild(pb2);
                pb2.setPosition(this.node.x+10,this.node.y+25);
            }
            else if(this.type==2)
            {
                var pb1 = cc.instantiate(this.playerBulletLeftPrefab);
                this.node.parent.addChild(pb1);
                pb1.setPosition(this.node.x-15,this.node.y+25);

                var pb2 = cc.instantiate(this.playerBulletPrefab);
                this.node.parent.addChild(pb2);
                pb2.setPosition(this.node.x,this.node.y+25);

                var pb3 = cc.instantiate(this.playerBulletRightPrefab);
                this.node.parent.addChild(pb3);
                pb3.setPosition(this.node.x+15,this.node.y+25);
            }
            else
            {
                var pb = cc.instantiate(this.playerBulletPrefab);
                this.node.parent.addChild(pb);

                pb.setPosition(this.node.x,this.node.y+25);
            }
},

3.生成敌人编队和敌人子弹
– 敌人编队

 spawnEnemy()
    {
        var max=4;
        var min=1;
        var n=Math.floor(Math.random() * (max - min + 1)) + min;
        if(n==1)
        {
            if(100<=this.node.parent.getComponent("Canvas").score&&this.node.parent.getComponent("Canvas").score<1200)
            {
                this.node.x=150-Math.random()*300;
                var enemy1=cc.instantiate(this.doubleEnemyPrefab1);
                this.node.parent.addChild(enemy1);
                enemy1.y=this.node.y;
                enemy1.x=this.node.x-20;

                var enemy2=cc.instantiate(this.doubleEnemyPrefab1);
                this.node.parent.addChild(enemy2);
                enemy2.y=this.node.y;
                enemy2.x=this.node.x+20;
            }
        }
       else if(n==2)
       {
           if(100<=this.node.parent.getComponent("Canvas").score&&this.node.parent.getComponent("Canvas").score<1200)
           {
                this.node.x=150-Math.random()*300;
                var enemy1=cc.instantiate(this. doubleEnemyPrefab1);
                this.node.parent.addChild(enemy1);
                enemy1.y=this.node.y;
                enemy1.x=this.node.x-20;

                var enemy2=cc.instantiate(this. doubleEnemyPrefab1);
                this.node.parent.addChild(enemy2);
                enemy2.y=this.node.y;
                enemy2.x=this.node.x+20;

                var enemy3=cc.instantiate(this. doubleEnemyPrefab2);
                this.node.parent.addChild(enemy3);
                enemy3.y=this.node.y-35;
                enemy3.x=this.node.x;
           }
       }
       else if(n==3)
       {
           if(100<=this.node.parent.getComponent("Canvas").score&&this.node.parent.getComponent("Canvas").score<1200)
           {
                this.node.x=150-Math.random()*300;
                var enemy1=cc.instantiate(this. doubleEnemyPrefab2);
                this.node.parent.addChild(enemy1);
                enemy1.y=this.node.y;
                enemy1.x=this.node.x-20;

                var enemy2=cc.instantiate(this. doubleEnemyPrefab2);
                this.node.parent.addChild(enemy2);
                enemy2.y=this.node.y;
                enemy2.x=this.node.x+20;

                var enemy3=cc.instantiate(this. doubleEnemyPrefab3);
                this.node.parent.addChild(enemy3);
                enemy3.y=this.node.y+35;
                enemy3.x=this.node.x;
           }
       }
       else if(n==4)
       {
           if(100<=this.node.parent.getComponent("Canvas").score&&this.node.parent.getComponent("Canvas").score<1200)
           {
                this.node.x=150-Math.random()*300;
                var enemy1=cc.instantiate(this. doubleEnemyPrefab1);
                this.node.parent.addChild(enemy1);
                enemy1.y=this.node.y;
                enemy1.x=this.node.x-20;

                var enemy2=cc.instantiate(this. doubleEnemyPrefab2);
                this.node.parent.addChild(enemy2);
                enemy2.y=this.node.y;
                enemy2.x=this.node.x+20;

                var enemy3=cc.instantiate(this. doubleEnemyPrefab3);
                this.node.parent.addChild(enemy3);
                enemy3.y=this.node.y+35;
                enemy3.x=this.node.x;

                var enemy4=cc.instantiate(this. doubleEnemyPrefab4);
                this.node.parent.addChild(enemy4);
                enemy4.y=this.node.y-35;
                enemy4.x=this.node.x;
           }
       }
    },
  • 敌人子弹
 spawnEnemyBullet()
    {
        var eb = cc.instantiate(this.enemyBulletPrefab);
        this.node.parent.addChild(eb);
        eb.setPosition(this.node.x,this.node.y-20);
    },

4.BOSS攻击

BOSS有几种攻击方式,每隔一段时间切换一个攻击方式

5.吃道具和子弹碰撞处理

 onPostSolve: function (contact, selfCollider, otherCollider)
    {
        if(otherCollider.node.name=='X1')
        {
            this.type=1;
            otherCollider.node.getComponent(cc.AudioSource).play();

        }
        if(otherCollider.node.name=='X2')
        {
            this.type=2;
            otherCollider.node.getComponent(cc.AudioSource).play();

        }
        if(otherCollider.node.name=='enemyBullet'||otherCollider.node.name=='enemyBulletLeft'||otherCollider.node.name=='enemyBulletLeft2'||otherCollider.node.name=='enemyBulletRight'||otherCollider.node.name=='enemyBulletRight2')
        {
            this.node.parent.getComponent("Canvas").playerHP();

        }
        if(otherCollider.node.name=='jiGuang')
        {

            this.node.parent.getComponent("Canvas").playerHP();

        }
        if(otherCollider.node.name=='fire')
        {

            this.node.parent.getComponent("Canvas").playerHP();

        }
        if(otherCollider.node.name=='boss')
        {

            this.node.parent.getComponent("Canvas").playerHP();

        }
        if(otherCollider.node.name=='e1')
        {

            this.node.parent.getComponent("Canvas").playerHP();

        }
        if(otherCollider.node.name=='e2')
        {

            this.node.parent.getComponent("Canvas").playerHP();

        }
        if(otherCollider.node.name=='e3')
        {

            this.node.parent.getComponent("Canvas").playerHP();

        }
        if(otherCollider.node.name=='e4')
        {

            this.node.parent.getComponent("Canvas").playerHP();

        }
        if(otherCollider.node.name=='e5')
        {

            this.node.parent.getComponent("Canvas").playerHP();

        }
    },

6.音乐音效和胜负判定

通过音效组件或者音效预制体添加音效。
打死BOSS过关,玩家血值为0则失败。

发表评论