When making banner ads, often times you need to condense your animations into a short period of time. I needed an easy way to see how long my animations were taking, but something that could be quickly turned off when I needed to publish for a client review. I came up with this simple QuickTimer class. Simply import the class, then create a new instance of QuickTimer, passing in a MovieClip. The class will create a little white box in the upper left corner that will count up the seconds. Alternately, you can pass in a textfield that it will use to display the timing, or you can pass in nothing, which will turn on traceMode to simply trace out the time.
package com{
import flash.text.TextField;
import flash.events.TimerEvent;
import flash.utils.Timer;
public class QuickTimer{
private var mainTimer:Timer;
private var _txt:TextField;
private var milliseconds:int;
private var strTime:String;
private var traceMode:Boolean = false;
public function QuickTimer(txt:* = null){//pass in either a textfield or movieClip to attach a textField
if(!txt){
traceMode = true;
}else{
if(txt is TextField){
_txt = txt;
_txt.parent.visible = true;
}else{
_txt = new TextField();
_txt.height = 15;
_txt.width = 26;
_txt.background = true;
txt.addChild(_txt);
txt.visible = true;
_txt.x = 5;
_txt.y = 5;
}
_txt.visible = true;
}
mainTimer = new Timer(100);
milliseconds = 0;
mainTimer.addEventListener(TimerEvent.TIMER, updateTime);
mainTimer.start();
}
private function updateTime(evt:TimerEvent):void{
milliseconds += 1;
var seconds:int = Math.floor(milliseconds/10);
var remaining:int = milliseconds - seconds*10;
strTime = seconds + ":" + remaining;
if(traceMode && remaining == 0){
trace("QuickTimer: " + seconds + " seconds");
}else{
_txt.text = strTime;
}
}
public function stopTimer(){
mainTimer.stop();
/*if(traceMode){
trace("QuickTimer end: " + strTime);
}*/
}
public function traceTime(timeLabel:String = null){
if(timeLabel){
trace(timeLabel + ": " + strTime);
}else{
trace("QuickTimer current time: " + strTime);
}
}
}
}