I was asked for an AS3 Flash counter displaying the time in seconds as they go by and seconds conversion into the proper hours : minutes : seconds display format.
To count the seconds, I used AS3 Timer class and for formatting I found this formatTime function of Jeff Depascale and put together following Flash counter using the Timer class.
Here’s the flash counter displaying time in minutes and seconds.
Following is the AS3 code and I have added comments to explain:
//time in milliseconds, set it to 1000 to display seconds var time:int = 10; //create the timer object var MyTimer:Timer = new Timer(time,0); MyTimer.start(); mc.addEventListener(Event.ENTER_FRAME, myCounter); function myCounter(e:Event) { mc.timer.text = String(formatTime(MyTimer.currentCount)); } /* This function formatTime was written by Jeff Depascale. Thanks Jeff! */ function formatTime( time:Number ):String { var remainder:Number; var hours:Number = time / ( 60 * 60 ); remainder = hours - (Math.floor ( hours )); hours = Math.floor(hours); var minutes = remainder * 60; remainder = minutes - (Math.floor ( minutes )); minutes = Math.floor(minutes); var seconds = remainder * 60; remainder = seconds - (Math.floor ( seconds )); seconds = Math.floor(seconds); var hString:String = hours < 10 ? "0" + hours:"" + hours; var mString:String = minutes < 10 ? "0" + minutes:"" + minutes; var sString:String = seconds < 10 ? "0" + seconds:"" + seconds; if (time < 0 || isNaN(time)){ return "00:00"; } if (hours > 0) { return hString + ":" + mString + ":" + sString; } else{ return mString + ":" + sString; } }
You can download the Counter fla file from this link
Hope that helps.
Cheers!
Can you stop the clock in some way?
Thanks Ali.
Hi Ali,
Thanks for the timer.
It might not be possible, I’m looking for something that will show (in minutes:seconds) how long a movieclip (7700 frames) is as it is being played through. Something similar to a youtube video showing how much time has played in the video (0:50 / 3:40). Do you know if that’s possible?
Thanks,
Sarah
That you can easily achieve, you just need to divide the total frames by the fps of the movie. You wil get total time in seconds. Then by getting the current frame, you can easily make a (min : seconds ) formatting a la youtube video timer.
I’ll try to code it when I get some time.