开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第17天,点击查看活动详情
上次我们创建了一个enemyBulletFactory脚本,今天我们继续来完善它。
1. 打开enemyBulletFactory脚本,去掉
@ccclass('enemyBulletFactory')
2. 继承gameFactory
import { _decorator, Component, Node } from 'cc';
import { gameFactory } from './gameFactory';
const { ccclass, property } = _decorator;
export class enemyBulletFactory extends gameFactory {
start() {
}
update(deltaTime: number) {
}
}
3. 打开常驻节点persistNode.ts脚本
(1)添加装饰器
@property(Prefab)
enemyBulletPrefab:Prefab=null;
(2)关联预制体
(3)添加敌机子弹装饰器 @property(SpriteFrame)
enemyBullet1:SpriteFrame=null; //敌机子弹1图片
@property(SpriteFrame)
enemyBullet2:SpriteFrame=null; //敌机子弹2图片
(4)关联敌机子弹图片
4. 打开脚本global.ts,添加常量
public static ENEMY_BULLET_1:string='enemybullet1'
public static ENEMY_BULLET_2:string='enemybullet2'
5. 添加创建敌机子弹的函数
public createProduct(productType:string): Node{
let enemyBulltTemp:Node=null;
if(this.productPool.size() >0){
enemyBulltTemp=this.productPool.get();//如果池子里有子弹,就直接拿来用
}else{ enemyBulltTemp=instantiate(this.persistNode.getComponent(persistNode).enemyBulletPrefab) //从常驻节点拿到预制体原料
}
switch(productType){
case global.ENEMY_BULLET_1: enemyBulltTemp.getComponent(enemyBullet).init(productType,this.persistNode.getComponent(persistNode).enemyBullet1);//通过调用enemyBullet的init方法来创建子弹
break;
case global.ENEMY_BULLET_2:
enemyBulltTemp.getComponent(enemyBullet).init(productType,this.persistNode.getComponent(persistNode).enemyBullet2);
break;
}
return enemyBulltTemp;
}
6. 打开常驻节点persistNode.ts脚本
(1)引入脚本
import { enemyBulletFactory } from './enemyBulletFactory';
(2)定义敌机子弹工厂
enemyBulletFactory:gameFactory=null; //敌机子弹工厂
(3)在onLoad函数实例化和
this.enemyBulletFactory= new enemyBulletFactory()
7. 打开enemy.ts脚本
(1)在onLoad函数里获取enemyBulletFactory
this.enemyBulletFactory=this.persistNode.getComponent(persistNode).enemyBulletFactory;
(2)定义敌机发射子弹计时器
enemy1ShootTimer:number=0;//敌机1发射子弹计时器
enemy2ShootTimer:number=0;//敌机1发射子弹计时器
(3)增加敌机1和敌机2的发射子弹函数
//敌机1发射子弹
enemy1Shoot(){
let postBegin:Vec3=new Vec3() //定义子弹开始的位置
let enemyBullet:Node=null;
enemyBullet= this.enemyBulletFactory.createProduct(global.ENEMY_BULLET_1)//制作普通子弹
this.node.parent.addChild(enemyBullet) //添加节点到画布
this.curPos=this.node.getPosition() //获取敌机当前位置
postBegin.x=this.curPos.x
postBegin.y=this.curPos.y-50 //设置到机头位置
enemyBullet.setPosition(postBegin)
}
//敌机2发射子弹
enemy2Shoot(){
let postBegin:Vec3=new Vec3() //定义子弹开始的位置
let enemyBullet:Node=null;
enemyBullet= this.enemyBulletFactory.createProduct(global.ENEMY_BULLET_2)//制作普通子弹
this.node.parent.addChild(enemyBullet) //添加节点到画布
this.curPos=this.node.getPosition() //获取敌机当前位置
postBegin.x=this.curPos.x
postBegin.y=this.curPos.y-50 //设置到机头位置
enemyBullet.setPosition(postBegin)
}
(4)在update函数里调用敌机发射子弹函数
update(deltaTime:number){
if(this.enemyType==global.ENEMY_1){
this.enemy1Move(deltaTime)
//敌机1发射子弹
this.enemy1ShootTimer+=deltaTime
if(this.enemy1ShootTimer >3){
this.enemy1Shoot();
this.enemy1ShootTimer=0;
} }else if(this.enemyType==global.ENEMY_2){
this.enemy2Move(deltaTime)
//敌机2发射子弹
this.enemy2ShootTimer+=deltaTime
if(this.enemy2ShootTimer >5){
this.enemy2Shoot();
this.enemy2ShootTimer=0;
}
}
}
今天就到这里了,主要说了敌机子弹发射功能,跟玩家飞机发射子弹差不到的。下一次再来说说敌机子弹的移动。可能写的过程中还有很多不好的地方,希望大家能指出来,在此,谢谢大家