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

128 阅读2分钟

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

上次说到了物资工厂的制作,今天我们继续来完善物资工厂。

1. 打开脚本persistNode.ts

(1)引入

import { goodsFactory } from './goodsFactory';

(2)定义物资工厂

    goodsFactory:gameFactory=null; //物资工厂

(3)在onLoad函数里实例化

        this.goodsFactory= new goodsFactory()

2. 打开脚本mainGame.ts

(1)定义物资工厂

    goodsFactory:gameFactory=null //物资工厂

(2)在onLoad函数里获取物资工厂

    this.goodsFactory=this.persistNode.getComponent(persistNode).goodsFactory

(3)引入global脚本

import { global } from './global';

(4)分别添加生产加血物资,生产激光物资,生产导弹物资函数

    //产生加血物资

    productBloodGoods(){

        let postBegin:Vec3=new Vec3();

        let bloodGoodsTeamp:Node=null;

        bloodGoodsTeamp=this.goodsFactory.createProduct(global.BLOODGOODS)

        this.node.addChild(bloodGoodsTeamp)

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

        postBegin.y=640

        bloodGoodsTeamp.setPosition(postBegin)

    }

    //产生激光物资

    productLightGoods(){

        let postBegin:Vec3=new Vec3();

        let lightGoodsTeamp:Node=null;

        lightGoodsTeamp=this.goodsFactory.createProduct(global.LIGHTGOODS)

        this.node.addChild(lightGoodsTeamp)

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

        postBegin.y=640

        lightGoodsTeamp.setPosition(postBegin)

    }

    //产生导弹物资

    productMissileGoods(){

        let postBegin:Vec3=new Vec3();

        let missileGoodsTeamp:Node=null;

        missileGoodsTeamp=this.goodsFactory.createProduct(global.MISSILEGOODS)

        this.node.addChild(missileGoodsTeamp)

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

        postBegin.y=640

        missileGoodsTeamp.setPosition(postBegin)

    }

(5)定义定时器

    bloodTimer:number=0;//产生加血物资的定时器

    lightTimer:number=0;//产生激光物资的定时器

    missileTimer:number=0;//产生导弹物资的定时器

  (6)定义物资随机多少秒

    bloodRandom:number=2+Math.random()*4 //加血物资随机多少秒

    lightRandom:number=2+Math.random()*4 //激光物资随机多少秒

    missileRandom:number=2+Math.random()*4 //导弹物资随机多少秒

(7)在update函数里分别调用生产加血物资,生产激光物资,生产导弹物资函数

        //产生加血物资

        this.bloodTimer+=deltaTime;

        if(this.bloodTimer>this.bloodRandom){

            this.productBloodGoods();

            this.bloodTimer=0;

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

        }

        //产生激光物资

        this.lightTimer+=deltaTime;

        if(this.lightTimer>this.lightRandom){

            this.productLightGoods();

            this.lightTimer=0;

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

        }

        //产生导弹物资

        this.missileTimer+=deltaTime;

        if(this.missileTimer>this.missileRandom){

            this.productMissileGoods();

            this.missileTimer=0;

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

        }  

3. 打开goods.ts脚本

(1)引入文件

import { gameFactory } from './gameFactory';

import { global } from './global';

import { persistNode } from './persistNode';

(2)定义物资工厂

    persistNode:Node=null;

    goodsFactory:gameFactory=null;

(3)在onLoad函数里获取物资工厂

    onLoad(){

        this.persistNode=find("PersistNode");

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

    }

(4)分别定义这三类物资的移动速度

    bloodGoodsMoveSpeed:number=0; //加血物资移动速度

    lightGoodsMoveSpeed:number=0; //激光物资移动速度

    missileGoodsMoveSpeed:number=0;//导弹物资移动速度

(5)在onLoad函数里初始化

        this.bloodGoodsMoveSpeed=100;

        this.lightGoodsMoveSpeed=100;

        this.missileGoodsMoveSpeed=100;  

(6)添加三种物资的移动函数

    //加血物资移动

    bloodGoodsMove(deltaTime:number){

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

        this.curPos.y-=this.bloodGoodsMoveSpeed * deltaTime;

        this.node.setPosition(this.curPos);

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

            this.goodsFactory.recycleProduct(this.node)

        }

    }

    //激光物资移动

    lightGoodsMove(deltaTime:number){

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

        this.curPos.y-=this.lightGoodsMoveSpeed * deltaTime;

        this.node.setPosition(this.curPos);

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

            this.goodsFactory.recycleProduct(this.node)

        }

    }

    //导弹物资移动

    missileGoodsMove(deltaTime:number){

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

        this.curPos.y-=this.missileGoodsMoveSpeed * deltaTime;

        this.node.setPosition(this.curPos);

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

            this.goodsFactory.recycleProduct(this.node)

        }

    }

(7)在update函数里调用物资移动函数

    update(deltaTime:number){

        if(this.goodsType==global.BLOODGOODS){

            this.bloodGoodsMove(deltaTime)    

        }else if(this.goodsType==global.LIGHTGOODS){

            this.lightGoodsMove(deltaTime)

        }else if(this.goodsType==global.MISSILEGOODS){

            this.missileGoodsMove(deltaTime)

        }

    } 

今天就到这里了,主要说物资的产生和移动。可能写的过程中还有很多不好的地方,希望大家能指出来,在此,谢谢大家