var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
/**
* @module annie
*/
var annie;
(function (annie) {
/**
* 定时器类
* @class annie.Timer
* @public
* @since 1.0.9
*/
var Timer = (function (_super) {
__extends(Timer, _super);
//Evetns
/**
* annie.Timer定时器触发事件
* @event annie.Event.TIMER
* @since 1.0.9
*/
/**
* annie.Timer定时器完成事件
* @event annie.Event.TIMER_COMPLETE
* @since 1.0.9
*/
/**
* 构造函数,初始化
* @method Timer
* @param {number} delay
* @param {number} repeatCount
* @example
* var timer=new annie.Timer(1000,10);
* timer.addEventListener(annie.Event.TIMER,function (e) {
* console.log("once");
* })
* timer.addEventListener(annie.Event.TIMER_COMPLETE, function (e) {
* console.log("complete");
* e.target.kill();
* })
* timer.start();
*/
function Timer(delay, repeatCount) {
if (repeatCount === void 0) { repeatCount = 0; }
_super.call(this);
this._currentCount = 0;
this._delay = 0;
this._frameDelay = 0;
this._currentFrameDelay = 0;
this._repeatCount = 0;
this._running = false;
if (delay <= 0) {
delay = 1;
}
var s = this;
s._delay = delay;
s._frameDelay = Math.ceil(delay * 0.001 * annie.Stage._FPS);
s._repeatCount = repeatCount;
Timer._timerList.push(s);
}
/**
* 重置定时器
* @method reset
* @public
* @since 1.0.9
*/
Timer.prototype.reset = function () {
var s = this;
s._running = false;
s._currentCount = 0;
s._currentFrameDelay = 0;
};
/**
* 开始执行定时器
* @method start
* @public
* @since 1.0.9
*/
Timer.prototype.start = function () {
var s = this;
s._running = true;
if (s._currentCount == s._repeatCount) {
s._currentCount = 0;
}
};
/**
* 停止执行定时器,通过再次调用start方法可以接着之前未完成的状态运行
* @method stop
* @public
* @since 1.0.9
*/
Timer.prototype.stop = function () {
this._running = false;
};
Object.defineProperty(Timer.prototype, "currentCount", {
/**
* 当前触发了多少次Timer事件
* @property currentCount
* @readonly
* @public
* @since 1.0.9
* @return {number}
*/
get: function () {
return this._currentCount;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Timer.prototype, "delay", {
/**
* 设置或者获取当前定时器之间的执行间隔
* @property delay
* @since 1.0.9
* @public
* @return {number}
*/
get: function () {
return this._delay;
},
set: function (value) {
this._delay = value;
this._frameDelay = Math.ceil(value * 0.001 * annie.Stage._FPS);
},
enumerable: true,
configurable: true
});
Object.defineProperty(Timer.prototype, "repeatCount", {
/**
* 执行触发Timer 的总次数
* @method repeatCount
* @public
* @since 1.0.9
* @return {number}
*/
get: function () {
return this._repeatCount;
},
set: function (value) {
if (value < 0) {
value = 0;
}
this._repeatCount = value;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Timer.prototype, "running", {
/**
* 当前是否在运行中
* @property running
* @since 1.0.9
* @return {boolean}
*/
get: function () {
return this._running;
},
enumerable: true,
configurable: true
});
/**
* 定时器不用了,一定要记得杀死它,不然他会变成厉鬼,时时缠绕着你
* @method kill
* @public
* @since 1.0.9
*/
Timer.prototype.kill = function () {
var len = Timer._timerList.length;
var s = this;
for (var i = 0; i < len; i++) {
if (Timer._timerList[i]._instanceId == s._instanceId) {
Timer._timerList.splice(i, 1);
break;
}
}
};
Timer.prototype.update = function () {
var s = this;
if (s._running) {
s._currentFrameDelay++;
if (s._currentFrameDelay == s._frameDelay) {
if (s._repeatCount) {
s._currentCount++;
}
s._currentFrameDelay = 0;
//触发事件
s.dispatchEvent("onTimer");
if (s._repeatCount && s._currentCount == s._repeatCount) {
//触发完成时事件
s._running = false;
s.dispatchEvent("onTimerComplete");
}
}
}
};
Timer.flush = function () {
var len = Timer._timerList.length;
for (var i = len - 1; i >= 0; i--) {
if (Timer._timerList[i]) {
Timer._timerList[i].update();
}
else {
Timer._timerList.splice(i, 1);
}
}
};
Timer.prototype.destroy = function () {
var s = this;
s.kill();
_super.prototype.destroy.call(this);
};
Timer._timerList = [];
return Timer;
}(annie.EventDispatcher));
annie.Timer = Timer;
})(annie || (annie = {}));