/*
* 技能CD冷却;
*/
var skillBtn = (function(_super) {
function skillBtn(params) {
skillBtn.super(this);
this.maxAngle = 360
this.angle = 0;
this.start = 0;
this.skin = params.imgPath;
this.pos(params.x,params.y)
if(!params.parent){
console.log("not find parent Node")
return
}
var parent = params.parent
parent.addChild(this);
this.bg = new Laya.Image();
this.bg.skin = params.imgPath;
this.bg.pos(params.x,params.y)
var sp = new Laya.Sprite();
parent.addChild(this.bg);
this.bg.mask = sp;
this.bg.visible = false;
this.spMask = sp; //用作画笔
}
Laya.class(skillBtn,"skillBtn",_super)
var _proto_ = skillBtn.prototype;
_proto_.setCdTime = function(cdTime){
//cd 时间
this.cdTime = cdTime
}
_proto_.startT = function(){
//释放技能时间
this.tickTime = new Date().getTime()
//总恢复真实时间
this.totalTime = this.tickTime + this.cdTime*1000
//剩余倒计时时间
this.retime = Math.ceil((this.totalTime - new Date().getTime())/1000)
//已经使用时间
this.useTime = this.cdTime - this.retime
//恢复已经计时的
this.angle += Math.ceil((this.maxAngle/this.cdTime)*this.useTime);
if(this.retime < 0){
return;
}
this.gray = true
this.bg.visible = true;
this.spMask.graphics.drawPie(this.bg.width/2, this.bg.height/2, this.bg.width/2, this.start, this.angle, "#ffffff");
Laya.timer.frameLoop(1, this, this._startCd.bind(this));
}
/**
* 开始倒计时CD
*/
_proto_._startCd = function(){
if (this.angle >= this.maxAngle) {
this._endCD()
return;
}
this.angle += (Laya.timer.delta/1000)*(360/this.retime);
console.log(this.angle)
this.spMask.graphics.clear();
this.spMask.graphics.drawPie(this.bg.width/2, this.bg.height/2, this.bg.width/2,
this.start, this.angle, "#ffffff");
}
/**
* 结束倒计时
*/
_proto_._endCD = function(){
Laya.timer.clearAll(this);
this.angle = 0
this.gray = false
this.bg.visible = false;
}
return skillBtn;
}(Laya.Image));
//使用只需要
var params = {
parent:Laya.stage,
x:100,
y:100,
imgPath: "common/skill.png"
}
var skillBtn = new skillBtn( params );
skillBtn.setCdTime(10)//设置CD时长 秒
skillBtn.startT();