- 用LayaAirIDE制作游戏暂停、关卡、积分、血量等UI界面.
- 用游戏代码来实现对UI的控制并且优化之前到逻辑.
/**
* 游戏UI类
*/
class GameInfo extends ui.GameInfoUI {
constructor() {
super();
//注册按钮点击事件,点击之后暂停游戏
this.pauseBtn.on(Laya.Event.CLICK,this,this.onPauseBtnClick);
//初始化UI显示
this.reset();
}
public reset():void{
this.infoLabel.text = "";
this.hp(5);
this.level(0);
this.score(0);
}
onPauseBtnClick(e:Laya.Event):void{
//阻止事件冒泡
e.stopPropagation();
//暂停游戏
this.infoLabel.text = "游戏已暂停,点击任意地方恢复游戏";
gameInstance.pause();
Laya.stage.once(Laya.Event.CLICK,this,this.onStageClick);
}
onStageClick(e:Laya.Event):void{
this.infoLabel.text = "";
gameInstance.resume();
}
//显示血量
public hp(value:number):void{
this.hpLabel.text = "HP:"+value;
}
//显示关卡级别
public level(value:number):void{
this.levelLabel.text = "Level:"+value;
}
//显示积分
public score(value:number):void{
this.scoreLabel.text = "Score:"+value;
}
}
/**
*主程序
*/
restart():void{
//重置游戏数据
this.score = 0;
this.level = 0;
this.levelUpScore = 10;
this.bulletLevel = 0;
this.gameInfo.reset();
//初始化角色
this.hero.init("hero",0,5,0,30);
//设置射击类型
this.hero.shootType = 1;
//设置角色位置
this.hero.pos(200,500);
//重置射击间隔
this.hero.shootInterval = 500;
//显示角色
this.hero.visible = true;
for(var i:number = this.roleBox.numChildren-1;i>-1;i--){
var role:Role = this.roleBox.getChildAt(i) as Role;
if(role != this.hero){
role.removeSelf();
//回收之前重置的信息
role.visible = true;
//回收到对象池
Laya.Pool.recover("role",role);
}
}
this.resume();
}
//暂停
public pause():void{
//停止游戏主循环
Laya.timer.clear(this,this.onLoop);
//移除舞台的鼠标移动事件
Laya.stage.off(Laya.Event.MOUSE_MOVE,this,this.onMouseMove);
}
//恢复
public resume():void{
//创建游戏主循环
Laya.timer.frameLoop(1,this,this.onLoop);
//监听舞台的鼠标移动事件
Laya.stage.on(Laya.Event.MOUSE_MOVE,this,this.onMouseMove);
}
- 给游戏添加音效
相应到地方调用Laya.SoundManager.playSound
//播放爆炸动画弹声音
Laya.SoundManager.playSound("res/sound/"+role.type+"_down.mp3");
- 增加屏幕适配
//设置适配模式
Laya.stage.scaleMode = "showall";
//设置居中对齐
Laya.stage.alignH = "center";
//设置横竖屏
Laya.stage.screenMode = "vertical";
完整源代码已上传