第二节 地鼠的显示、停留、消失、受击
Game.js
var Game=(function(_super){
function Game(){
Game.super(this);
this.mole=new Mole(this.normal,this.hit,22);
Laya.timer.loop(2000,this,this.onLoop);
}
//注册类
Laya.class(Game,"Game",_super);
var _proto=Game.prototype;
_proto.onLoop=function(){
this.mole.show();
}
return Game;
})(ui.GameUI);
Mole.js
var Mole = (function(){
function Mole(normalState,hitState,downY){
this.normalState=normalState;
this.hitState=hitState;
this.downY=downY;
this.upY=this.normalState.y;
this.reset();
this.normalState.on(Laya.Event.CLICK,this,this.hit);
};
var _proto=Mole.prototype;
//重置
_proto.reset=function(){
this.normalState.visible=false;
this.hitState.visible=false;
this.isVctive=false;
this.isShow=false;
this.isHit=false;
}
//显示
_proto.show=function(){
if(this.isVctive) return;
this.isVctive=true;
this.isShow=true;
this.normalState.y=this.downY;
this.normalState.visible=true;
// this.hitState.visible=false;
Laya.Tween.to(this.normalState,{y:this.upY},500,Laya.Ease.backOut,Laya.Handler.create(this,this.showComplete))
}
//停留
_proto.showComplete=function(){
if(this.isShow && !this.isHit){
Laya.timer.once(2000,this,this.hide)
}
}
//消失
_proto.hide=function(){
if(this.isShow && !this.isHit){
this.isShow=false;
Laya.Tween.to(this.normalState,{y:this.downY},300,Laya.Ease.backIn,Laya.Handler.create(this,this.reset))
}
}
//受击
_proto.hit=function(){
if(this.isShow &&!this.isHit){
this.isShow=false;
this.isHit=true;
Laya.timer.clear(this,this.hide);
this.normalState.visible=false;
this.hitState.visible=true;
Laya.timer.once(500,this,this.reset);
}
}
return Mole;
})();
到此实现第一个地洞循环出现地鼠 打它!