LayaAir飞机大战-4

  1. 添加游戏资源并缓存动画.

//缓存子弹动画

Laya.Animation.createFrames(["war/bullet1.png"],"bullet1_fly");

  1. 增加子弹射击的基础属性.

//射击类型

public shootType:number = 0;

//射击间隔

public shootInterval:number = 500;

//下次射击时间

public shootTime:number = Laya.Browser.now()+2000;

//当前动作

public action:string = "";

//是否是子弹

public isBullet:boolean = false;

  1. 实现发射子弹的逻辑.

onLoop():void{

//遍历舞台上所有的飞机,更改飞机的状态

for(var i:number = this.roleBox.numChildren-1;i>-1;i--){

var role:Role = this.roleBox.getChildAt(i) as Role;

//处理发射子弹逻辑

if(role.shootType>0){

//获取当前时间

var time:number = Laya.Browser.now();

//如果当前时间大于下次射击时间

if(time>role.shootTime){

//更新下次射击时间

role.shootTime = time + role.shootInterval;

//根据不同子弹类型,设置不同的数量及位置

var pos:Array<number> = this.bulletPos[role.shootType - 1];

for(var index:number = 0;index < pos.length;index++){

//从对象池中创建一个子弹

var bullet:Role = Laya.Pool.getItemByClass("role",Role);

//初始化子弹信息

bullet.init("bullet1",role.camp,1,-4-role.shootType - Math.floor(this.level / 15),1,1);

// //设置角色类型为子弹类型

// bullet.isBullet = true;

//设置子弹的位置

bullet.pos(role.x + pos[index],role.y - role.hitRadius-10);

//添加到舞台上

this.roleBox.addChild(bullet);

}

}

}

}}

  1. 实现子弹与飞机到碰撞逻辑.

//检测碰撞

for(var i:number = this.roleBox.numChildren-1;i>-1;i--){

//获取角色对象1

var role1:Role = this.roleBox.getChildAt(i) as Role;

if(role1.hp<1)continue;

for(var j:number = i-1;j>-1;j--){

//如果角色已经死亡,则忽略

if(!role1.visible)continue;

//获取角色对象2

var role2:Role = this.roleBox.getChildAt(j) as Role;

//如果角色未死亡,并且阵营不同才能进行碰撞

if(role2.hp>0 && role1.camp != role2.camp){

//计算碰撞区域

var hitRadius:number = role1.hitRadius + role2.hitRadius;

//根据距离判断是否碰撞

if(Math.abs(role1.x - role2.x) < hitRadius && Math.abs(role1.y - role2.y) < hitRadius){

//碰撞之后掉血

this.lostHp(role1,1);

this.lostHp(role2,1);

}

}

}

}

lostHp(role:Role,lostHp:number):void{

//减血

role.hp-=lostHp;

}

 

/**

*角色类

*/

//添加动画播放完成事件

this.body.on(Laya.Event.COMPLETE,this,this.onPlayComplete);

onPlayComplete():void{

//如果是击毁动画,则隐藏对象

if(this.action === "down"){

//停止动画播放

this.body.stop();

//隐藏显示

this.visible = false;

}

else if(this.action === "hit"){

//如果是被击动画播放完毕,则接着播放飞行动画

this.playAction("fly");

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值