/**
* 角色类
*/
class Role extends Laya.Sprite {
//定义飞机的身体
private body:Laya.Animation;
//是否缓存了动画.
private static cached:boolean = false;
//角色类型
public type:string;
//阵营
public camp:number;
//血量
public hp:number;
//速度
public speed:number;
//被击半径
public hitRadius:number;
constructor() {
super();
//初始化
// this.init();
}
public init(_type:string,_camp:number,_hp:number,_speed:number,_hitRadius:number):void{
//缓存了无需重复缓存
if(!Role.cached){
Role.cached = true;
//缓存飞行动画
Laya.Animation.createFrames(["war/hero_fly1.png","war/hero_fly2.png"],"hero_fly");
//缓存击中爆炸效果
Laya.Animation.createFrames(["war/hero_down1.png","war/hero_down2.png"
,"war/hero_down3.png","war/hero_down4.png"],"hero_down");
//缓存敌机1飞行动画
Laya.Animation.createFrames(["war/enemy1_fly1.png"],"enemy1_fly");
//缓存敌机1爆炸动作
Laya.Animation.createFrames(["war/enemy1_down1.png","war/enemy1_down2.png","war/enemy1_down3.png",
"war/enemy1_down4.png"],"enemy1_down");
//缓存敌机2飞行动画
Laya.Animation.createFrames(["war/enemy2_fly1.png"],"enemy2_fly");
//缓存敌机2爆炸动作
Laya.Animation.createFrames(["war/enemy2_down1.png","war/enemy2_down2.png","war/enemy2_down3.png",
"war/enemy2_down4.png"],"enemy2_down");
//缓存敌机2碰撞动作
Laya.Animation.createFrames(["war/enemy2_hit.png"],"enemy2_hit");
//缓存敌机3飞行动画
Laya.Animation.createFrames(["war/enemy3_fly1.png","war/enemy3_fly2.png"],"enemy3_fly");
//缓存敌机3爆炸动作
Laya.Animation.createFrames(["war/enemy3_down1.png","war/enemy3_down2.png","war/enemy3_down3.png",
"war/enemy3_down4.png","war/enemy3_down5.png","war/enemy3_down6.png"],"enemy3_down");
//缓存敌机3碰撞动作
Laya.Animation.createFrames(["war/enemy3_hit.png"],"enemy3_hit");
}
//创建了无需重复创建
if(!this.body){
//创建一个动画作为飞机的身体
this.body = new Laya.Animation();
//把机体添加到容器内
this.addChild(this.body);
}
//播放飞机动画
this.playAction("fly");
}
}
playAction(action:string):void{
//根据类型播放动画
this.body.play(0,true,this.type+"_"+action);
//获取动画大小区域
var bound:Laya.Rectangle = this.body.getBounds();
//设置机身居中
this.body.pos(-bound.width/2,-bound.height/2);
}
//敌机血量
private hps:Array
//敌机速度
private speeds:Array
//敌机被击半径
private radius:Array
createEnemy(type:number,num:number,speed:number,hp:number):void{
for(var i:number = 0;i //随机出现敌人 var r:number = Math.random(); //根据随机数,随机敌人 var type:number = r<0.7?0:r<0.95?1:2; //创建敌人 var enemy:Role = Laya.Pool.getItemByClass("role",Role); //初始化角色 enemy.init("enemy"+(type+1),1,hp,speed,this.radius[type]); //随机位置 enemy.pos(Math.random()*400+40,-Math.random()*200 - 100); //添加到舞台上 this.roleBox.addChild(enemy); } } onLoop():void{ //遍历舞台上所有的飞机,更改飞机的状态 for(var i:number = this.roleBox.numChildren-1;i>-1;i--){ var role:Role = this.roleBox.getChildAt(i) as Role; if(role && role.speed){ //根据飞机速度改变位置 role.y+=role.speed; //如果敌机移动到显示区域外则移除 if(role.y>1000 || !role.visible || (role.isBullet && role.y<-20)){ //从舞台移除 role.removeSelf(); } } // //每隔30帧创建新的敌机 if(Laya.timer.currFrame%60 === 0){ 测试 this.createEnemy(2,1,3,1); } }