Layabox引擎开发H5打地鼠游戏(二)

第二节 地鼠的显示、停留、消失、受击

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;
})();

到此实现第一个地洞循环出现地鼠 打它!

Layabox引擎开发H5打地鼠游戏(一)构造打地鼠界面

猜你喜欢

转载自blog.csdn.net/qq_40184652/article/details/81483938