/*
//timer类as1.0版本
//----用例----
#include "Timer1.0.as"
T = new Timer(0.25*60*1000);
T.addViewer(TF1,"MSEL");//TF1显示毫秒数
T.addViewer(TF2,"SECOND");//TF2显示秒数
T.addViewer(TF3,"MM:SS");//TF3显示“分:秒”
T.addViewer(MC1);//MC1随时间正常播放
T.addViewer(MC2,-1);//MC2随时间倒序播
T.addViewer(MC3);//MC1随时间正常播放
T.onTimeOver = function() {
//时间结束
trace("time up")
this.reset();
};
T.onTimeRun = function() {
//计时中...
};
//开始
T.start();
//----------------
*/
function Timer(time) {
this.totalTime = time;
this.hasTime = time;
this.onTimeOver = null;
this.onTimeRun = null;
this.timeViewer = new Array();
_global._TimerAS1_IntervalID = null;
}
//----------开始,暂停,继续,重置,结束-------------
Timer.prototype.run = function() {
clearInterval(_global._TimerAS1_IntervalID);
var startTime = getTimer();
var mode = this;
_global._TimerAS1_IntervalID = setInterval(function () {
var lostTime = getTimer()-startTime;
mode.hasTime -= lostTime;
if (mode.hasTime<=0) {
mode.hasTime = 0;
mode.timeOver();
} else {
mode.onTimeRun();
}
mode.viewTime();
startTime = getTimer();
}, 0);
};
Timer.prototype.start = function() {
this.reset();
};
Timer.prototype.reset = function() {
this.hasTime = this.totalTime;
this.run();
};
Timer.prototype.pause = function() {
clearInterval(_global._TimerAS1_IntervalID);
};
Timer.prototype.proceed = function() {
this.run();
};
Timer.prototype.timeOver = function() {
clearInterval(_global._TimerAS1_IntervalID);
this.onTimeOver();
};
//------------时间显示器----------------
Timer.prototype.addViewer = function(obj, style) {
if ((obj instanceof MovieClip) || (obj instanceof TextField)) {
this.timeViewer.push({obj:obj, style:style});
}
};
Timer.prototype.delViewer = function(obj) {
for (var i in this.timeViewer) {
if (obj == this.timeViewer[i].obj) {
this.timeViewer.splice(i, 1);
}
}
};
Timer.prototype.clsViewer = function() {
this.timeViewer = new Array();
};
Timer.prototype.viewTime = function() {
for (var i in this.timeViewer) {
var obj = this.timeViewer[i].obj;
var style = this.timeViewer[i].style;
if (obj instanceof MovieClip) {
var num = Math.ceil((this.totalTime-this.hasTime)/this.totalTime*obj._totalframes);
if (style == -1) {
num = obj._totalframes-num;
}
obj.gotoAndStop(num);
} else if (obj instanceof TextField) {
obj.text = this.timeToString(style);
}
}
};
//-----------------------------------------------
Timer.prototype.timeToString = function(style) {
var str;
switch (style) {
case "SECOND" :
str = Math.ceil(this.hasTime/1000);
break;
case "MSEL" :
str = Math.ceil(this.hasTime);
break;
case "MM:SS" :
default :
var num = Math.ceil(this.hasTime/1000);
var MM = Math.floor(num/60);
var SS = Math.floor(num%60);
str = String("000"+MM).substr(-2)+":"+String("000"+SS).substr(-2);
break;
}
return String(str);
};
//查看所有成员(测试用)
Timer.prototype.listMembers = function() {
for (var i in this) {
trace(i+" = "+this[i]);
}
};