代码如下,在 onLoaded 里注册的开始游戏按钮事件,点击 执行gameInit添加hero,这时候界面没有东西;当三秒后游戏结束,点重新开始,飞机才出来
function onLoaded() {
start = new GameStartUI();
start.btn_start.on(Event.MOUSE_UP,this,gameInit)
Laya.stage.addChild(start);
}
function gameInit() {
map = new GameBgUI();
Laya.stage.addChild(map);
play = new GamPlayUI();
Laya.stage.addChild(play);
hero = new Hero("hero", 25)
hero.pos(100,200)
Laya.stage.addChild(hero)
Laya.timer.once(3000, this, gameOver)
}
function gameOver() {
map.removeSelf()
play.removeSelf()
over = new GameOverUI()
over.txt_score.text = "25"
over.btn_restart.on(Event.MOUSE_UP,this,gameInit)
Laya.stage.addChild(over);
}
hero类:
(function() {
function Hero(type, hp) {
Hero.__super.call(this);
this.fly();
}
Laya.class(Hero,"Hero", Air);
var _proto = Hero.prototype;
_proto.fly = function() {
this.playAction("fly")
}
})();
air类:
(function() {
function Air(type, hp) {
this.hp = hp;
this.type = type;
this.body = null;
Air.__super.call(this);
this.init();
}
Laya.class(Air,"Air", laya.display.Sprite);
var _proto = Air.prototype;
_proto.init = function() {
if(this.body == null) {
this.body = new laya.display.Animation();
this.addChild(this.body)
this.body.loadAnimation("GameRole.ani")
}
}
_proto.playAction = function(action){
//如果是重复的动作 不执行
if(this.action == action)return;
this.action = action;
this.body.play(0, true,this.type+"_" + this.action);
}
})();