package ca.ryac { import flash.display.Sprite; import flash.text.TextField; import flash.text.TextFormat; import flash.text.TextFieldAutoSize; import flash.events.Event; import flash.utils.getTimer; /** * StopWatch class. Displays a timer/stopwatch on the stage that you can * start/stop/resume/reset, edit text formatting, and get the time in milliseconds.. * * 3/28/2008 * * @author Ryan Yacyshyn * @version 1.0 */ public class StopWatch extends Sprite { private var _time:uint; private var timerOffset:uint; private var tf:TextField; private var format:TextFormat; /** * constructor.. * @param txtFormat sets the defaultTextFormat * @param embedFont use embedded fonts exported in the swf */ public function StopWatch (txtFormat:TextFormat = null, embedFont:Boolean = false):void { init (txtFormat, embedFont); } private function init (txtFormat:TextFormat, embedFont:Boolean):void { tf = new TextField (); tf.selectable = false; tf.autoSize = TextFieldAutoSize.LEFT; if (txtFormat != null) setTextFormat (txtFormat, embedFont); addChild (tf); reset (); } private function updateTime (e:Event):void { _time = getTimer () - timerOffset; formatTime (); } private function formatTime ():void { // get milliseconds.. var nMilli:uint = Math.floor(Math.floor(_time / 10) % 100); // sec & min.. var counter:uint = Math.floor(_time / 1000); var nSecs:uint = counter % 60; var nMins:uint = Math.floor (counter / 60); // formatting.. var sMilli:String = (nMilli < 10) ? sMilli = "0" + nMilli.toString() : nMilli.toString(); var sSecs:String = (nSecs < 10) ? sSecs = "0" + nSecs.toString() : nSecs.toString(); var sMins:String = (nMins < 10) ? sMins = "0" + nMins.toString() : nMins.toString(); // displaying.. tf.text = sMins + ":" + sSecs + ":" + sMilli; } /** * sets the text format for the stopwatch.. * @param txtFormat sets the defaultTextFormat to this TextFormat * @param embedFont use embedded fonts exported in the swf */ public function setTextFormat (txtFormat:TextFormat, embedFont:Boolean):void { tf.embedFonts = embedFont; tf.defaultTextFormat = txtFormat; tf.text = tf.text; } /** * starts the stopwatch.. */ public function start ():void { timerOffset = getTimer () - _time; addEventListener (Event.ENTER_FRAME, updateTime); } /** * resets the stopwatch.. */ public function reset ():void { _time = 0; timerOffset = getTimer (); formatTime (); } /** * stops the stopwatch.. */ public function stop ():void { removeEventListener (Event.ENTER_FRAME, updateTime); } /** * returns the time in milliseconds. use this number for storing * so that it can be formatted in different ways if needed later.. */ public function get time ():uint { return _time; } /** * formats the time from milliseconds to something more readable.. * @param the time in milliseconds */ public static function parseTime (raw:uint):String { var str:String; // millisecs.. var milli:uint = raw; // get milliseconds.. var nMilli:uint = Math.floor(Math.floor(milli / 10) % 100); // sec & min.. var counter:uint = Math.floor(milli / 1000); var nSecs:uint = counter % 60; var nMins:uint = Math.floor (counter / 60); // formatting.. var _sMilli:String = (nMilli < 10) ? "0" + nMilli.toString() : nMilli.toString(); var _sSecs:String = (nSecs < 10) ? "0" + nSecs.toString() : nSecs.toString(); var _sMins:String = (nMins < 10) ? "0" + nMins.toString() : nMins.toString(); str = _sMins + ":" + _sSecs + ":" + _sMilli; return str; } } }