Created
March 20, 2012 09:12
-
-
Save Tomyail/2133175 to your computer and use it in GitHub Desktop.
"万能"计时器
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package | |
{ | |
import flash.events.TimerEvent; | |
import flash.utils.Timer; | |
/** | |
* 计时器 | |
* @author lixuexin | |
* | |
*/ | |
public class Clock | |
{ | |
public function Clock() | |
{ | |
_increase = true; | |
_timer = new Timer(1000); | |
_timer.addEventListener(TimerEvent.TIMER,onTimer); | |
} | |
private var _increase:Boolean; | |
private var _length:int = 0; | |
private var _time:int; | |
private var _timer:Timer; | |
private var data:Vector.<int> = Vector.<int>([60,60,24,30]); | |
public function set increase(b:Boolean):void | |
{ | |
_increase = b; | |
} | |
public function get passedTime():int | |
{ | |
return _time; | |
} | |
public function pause():void | |
{ | |
_timer.stop(); | |
} | |
public function reset():void | |
{ | |
_time = 0 | |
_timer.reset(); | |
} | |
public function setPassedTime(passedTime:int,length:int = 3):void | |
{ | |
if(passedTime < 0) return; | |
_time = passedTime; | |
_length = length; | |
var result:String = ""; | |
var times:Vector.<String> = resolveByVec(_time,data); | |
var i:int = 0; | |
if(length >= times.length) | |
{ | |
for(i = length-1;i>-1;i--) | |
{ | |
if(times.length <= i) | |
{ | |
result +="00:" | |
} | |
else | |
{ | |
result += times[i]+":" | |
} | |
} | |
} | |
else | |
{ | |
//累积量 | |
var t:int = 0; | |
for(i = times.length-1;i-_length>-1;i--) | |
{ | |
t = (parseInt(times[i])+t) *data[i-1]; | |
} | |
times[times.length-1-times.length+_length] = (parseInt(times[times.length-1-times.length+_length])+t).toString(); | |
times.length = times.length-times.length+_length; | |
for(i = length-1;i>-1;i--) | |
{ | |
if(times.length <= i) | |
{ | |
result +="00:" | |
} | |
else | |
{ | |
result += times[i]+":" | |
} | |
} | |
} | |
result = result.substring(0,result.length-1); | |
trace(result) | |
// txt_time.text = result; | |
} | |
public function start():void | |
{ | |
_timer.start(); | |
} | |
/** | |
* 增加前缀或者后缀 | |
* @param num | |
* @param min | |
* @param max | |
* @param type | |
* @param flag | |
* @return | |
*/ | |
private function addFix(num:int,min:int = -1,max:int = 10,type:int = 0,flag:String = "0"):String | |
{ | |
if(num > min && num < max) | |
{ | |
if(type == 0) | |
{ | |
return flag + num; | |
} | |
else | |
{ | |
return num +flag; | |
} | |
} | |
return num.toString(); | |
} | |
private function onTimer(e:TimerEvent):void | |
{ | |
_increase ? _time ++:_time--; | |
setPassedTime(_time,_length) | |
} | |
private function resolve(number:int,base:int = 60):Vector.<String> | |
{ | |
var result:Vector.<String> = new Vector.<String>(); | |
var tt:int; | |
while(number >= base) | |
{ | |
tt = number % base; | |
result.push(addFix(tt)); | |
number /= base; | |
} | |
result.push(addFix(number)); | |
return result; | |
} | |
private function resolveByVec(number:int,baseVec:Vector.<int>):Vector.<String> | |
{ | |
var result:Vector.<String> = new Vector.<String>(); | |
var tt:int; | |
var index:int = 0; | |
while(number >= baseVec[index]) | |
{ | |
tt = number % baseVec[index]; | |
result.push(addFix(tt)); | |
number /= baseVec[index]; | |
index++; | |
} | |
result.push(addFix(number)); | |
return result; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment