从0开始用CocosCreator开发飞机大战小游戏(七)

111 阅读2分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第15天,点击查看活动详情

上一次说了敌机工厂的建造,但还没有完善,今天来完善下。

1. 打开mainGame.ts脚本,完善该脚本。

import { _decorator, Component, Node, find, Vec3 } from 'cc';

import { gameFactory } from './gameFactory';

import { persistNode } from './persistNode';

import { global } from './global';

const { ccclass, property } = _decorator;

@ccclass('mainGame')

export class min extends Component {

    enemyFactory:gameFactory=null //敌机工厂

    persistNode:Node=null; //持久节点

    enemy1Timer:number=0; //产生敌机1的定时器

    enemy2Timer:number=0;//产生敌机2的定时器

    enemy1Random:number=2+Math.random()*4

    enemy2Random:number=4+Math.random()*4

    onLoad(){

        this.persistNode=find("PersistNode");

        this.enemyFactory=this.persistNode.getComponent(persistNode).enemyFactory

    }

    //产生敌机1

    productEnemy1(){

        let postBegin:Vec3=new Vec3();

        let enemyTeamp:Node=null;

        enemyTeamp=this.enemyFactory.createProduct(global.ENEMY_1)

        this.node.addChild(enemyTeamp)

        postBegin.x=((Math.random()-0.5)*2)*360

        postBegin.y=640

        console.log(postBegin)

        enemyTeamp.setPosition(postBegin)

    }

    //产生敌机2

    productEnemy2(){

        let postBegin:Vec3=new Vec3();

        let enemyTeamp:Node=null;

        enemyTeamp=this.enemyFactory.createProduct(global.ENEMY_2)

        this.node.addChild(enemyTeamp)

        postBegin.x=((Math.random()-0.5)*2)*360

        postBegin.y=640

        enemyTeamp.setPosition(postBegin)

    }

      update(deltaTime: number) {

        this.enemy1Timer+=deltaTime;

        if(this.enemy1Timer>this.enemy1Random){

            this.productEnemy1();

            this.enemy1Timer=0;

            this.enemy1Random=2+Math.random()*4                 }

        this.enemy2Timer+=deltaTime;

        if(this.enemy2Timer>this.enemy2Random){

            this.productEnemy2();

            this.enemy2Timer=0;

            this.enemy2Random=4+Math.random()*4

        }

    }   

}

 

2. 敌机的移动。

(1)打开脚本enemy.ts引入以下脚本

import { gameFactory } from './gameFactory';

import { global } from './global';

import { persistNode } from './persistNode';

  (2)定义

    curPos:Vec3=null;

    enemyFactory:gameFactory=null;

    persistNode:Node=null;

(3)在onLoad获取敌机工厂

    onLoad(){

        this.persistNode=find("PersistNode");

        this.enemyFactory=this.persistNode.getComponent(persistNode).enemyFactory;

    }  

(4)增加敌机函数

    //敌机1移动

    enemy1Move(deltaTime:number){

        this.curPos=this.node.getPosition();

        this.curPos.y-=120*deltaTime;

        this.node.setPosition(this.curPos);

        if(this.curPos.y < -650){

            this.enemyFactory.recycleProduct(this.node)

        }

    }

      //敌机2移动

    enemy2Move(deltaTime:number){

        this.curPos=this.node.getPosition();

        this.curPos.y-=150*deltaTime;

        this.node.setPosition(this.curPos);

        if(this.curPos.y < -650){

            this.enemyFactory.recycleProduct(this.node)

        }

    }

  (5)在update函数里调用敌机移动函数,这样敌机的移动功能就做完了

    update(deltaTime:number){

        if(this.enemyType==global.ENEMY_1){

            this.enemy1Move(deltaTime)

        }else if(this.enemyType==global.ENEMY_2){

            this.enemy2Move(deltaTime)

        }

    }

3. 接下来我们说下子弹与敌机的碰撞。

(1)设置项目的物理系统为内置系统,用这个简单的物理系统就可以了 image.png

(2)打开playerBullet预制体,添加碰撞组件

image.png 

(3)记得点击保存 image.png

(4)同样,敌机也要添加碰撞组件,打开enemy预制体,添加碰撞组件 image.png

(5)同样也要记得保存   image.png

4. 分组

(1)添加分组 image.png

(2)添加Enemy和PlayerBullet这两个分组 image.png

(3)这里选择钩选,为了防止敌机跟敌机碰撞,子弹跟子弹碰撞 image.png

  (4)打开预制体enemy,分组选择Enemy。记得保存

image.png

(5)打开预制体playerBullet,分组选择PlayerBullet。记得保存 image.png

今天就到这里了,主要说了碰撞的一些设置,但还没有完善,下一次再来说说怎样完善这个碰撞功能。可能写的过程中还有很多不好的地方,希望大家能指出来,在此,谢谢大家