开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第18天,点击查看活动详情
上一次说到了敌机子弹的发射,但敌机的子弹的移动功能还没有做,今天就说下这个
1. 敌机子弹的移动
(1)引入脚本
import { gameFactory } from './gameFactory';
import { global } from './global';
import { persistNode } from './persistNode';
(2)定义敌机子弹工厂和常驻节点
enemyBulletFactory:gameFactory=null;
persistNode:Node=null;
(3)在onLoad函数里获取敌机子弹工厂
onLoad(){
this.persistNode=find("PersistNode") this.enemyBulletFactory=this.persistNode.getComponent(persistNode).enemyBulletFactory;
}
(4)定义敌机子弹类型
enemyBulletType:string=null; //敌机子弹类型
(5)敌机子弹初始化
init(enemyBulletType:string,SpriteFrame:SpriteFrame){
this.enemyBulletType=enemyBulletType;
this.node.getComponent(Sprite).spriteFrame=SpriteFrame;
}
(6)定义敌机子弹当前位置
curPos:Vec3=null; //当前子弹位置
isleft:boolean=true
(7)添加敌机子弹移动函数
//敌机子弹1移动
enemyBullet1Move(deltaTime: number){
this.curPos=this.node.getPosition();
this.curPos.y -=500*deltaTime
//子弹水平方移动
if(this.isleft){
this.curPos.x-=500 * deltaTime
if(this.curPos.x< -360){
this.isleft=false
}
}else{
this.curPos.x += 500 * deltaTime
if(this.curPos.x> 360){
this.isleft=true
}
}
this.node.setPosition(this.curPos)
if(this.curPos.y<- 640 ){
this.enemyBulletFactory.recycleProduct(this.node)
}
}
//敌机子弹2移动
enemyBullet2Move(deltaTime: number){
this.curPos=this.node.getPosition();
this.curPos.y -=500*deltaTime
this.node.setPosition(this.curPos)
if(this.curPos.y<- 640 ){
this.enemyBulletFactory.recycleProduct(this.node)
}
}
(7)在update函数里调用敌机子弹移动函数,这样敌机子弹移动功能就做好了
update(deltaTime: number) {
if(this.enemyBulletType==global.ENEMY_BULLET_1){
this.enemyBullet1Move(deltaTime)
}else if(this.enemyBulletType==global.ENEMY_BULLET_2){
this.enemyBullet2Move(deltaTime)
}
}
2. 接下来说下血条功能
(1)打开game场景,在play节点下新建一个progressBar(进度条),并且命为blood
(2) 调整blood的位置,设置position 的Y为-50,Content Size的宽为100,Total Length也是为100
(3)调整blood下的Bar位置,设置position 的Y为-50,Content Size的宽为100,颜色设置为红色。
3.添加碰撞分组
(1)打开项目设置
(2)添加EnemyBullet和Player分组
4.敌机子弹添加碰撞组件
(1)打开敌机子弹预制体,添加碰撞组件
(2)钩选Editing,这样就可以调范围。最后记得保存
(3)分组选择EnemyBullet
5. 玩家飞机添加碰撞组件
(1)打开play节点,添加碰撞组件
(2)分组选择Player
今天就到这里了,主要说了玩家飞机血条功能,但只说了一些基本的设置,下一次再来完善这个功能。可能写的过程中还有很多不好的地方,希望大家能指出来,在此,谢谢大家