


//initial variables var timing:Boolean = false; var paused:Boolean = false; var remaining:Number; var elapsedTime:Number; var elapsedHours: Number; var elapsedM:Number; var elapsedS:Number; var elapsedH:Number; var startTime:Number; var remaining:Number; var hours:String; var minutes:String; var seconds:String; var hundredths:String;All we are doing here is setting up all of the variables we are going to need to make our timer work. If it looks like too much right now, don't worry, it's really quite simple.
_root.play_btn.onPress = function() {
if(!_root.timing) {
if (_root.paused) {
_root.startTime = getTimer() - _root.elapsedTime;
} else {
_root.startTime = getTimer();
}
//start timer
_root.paused = false;
_root.timing = true;
}
}
getTimer() simply outputs the time in miliseconds since the flash movie was opened. Since the timer doesn't start as soon as our movie starts we will need to offset the timer with the
value of startTime. This will tell flash how long the movie was open until we started the timer so we can subtract it later.
_root.pause_btn.onPress = function() {
//only pause if the timer is actually going
if(_root.timing) {
_root.timing = false;
_root.paused = true;
}
}
all we are doing here is finding out if the timer is going. If it is we will set the timer to false and set our paused value to true.
_root.stop_btn.onPress = function() {
//stop the timer
_root.timing = false;
//reset the paused variable
_root.paused = false;
//reset the display textbox
_root.timer_txt = "00:00:00:00";
}
By pressing the stop button we are going to reset the timer to 0 seconds again. So first we will tell flash to stop timing, make sure our paused value is set to false, and reset
the display so that it shows 0 seconds.
_root.onEnterFrame = function() {
if (timing) {
//calculate values
elapsedTime = getTimer()-startTime;
//hours
elapsedHours = Math.floor(elapsedTime/3600000);
remaining = elapsedTime-(elapsedHours*3600000);
//minutes
elapsedM = Math.floor(remaining/60000);
remaining = remaining-(elapsedM*60000);
//seconds
elapsedS = Math.floor(remaining/1000);
remaining = remaining-(elapsedS*1000);
//hundredths
elapsedH = Math.floor(remaining/10);
//output to text box
//add a 0 on the front of the numbers
//if the number is less than 10
if (elapsedHours<10) {
hours = "0"+elapsedHours.toString();
} else {
hours = elapsedHours.toString();
}
if (elapsedM<10) {
minutes = "0"+elapsedM.toString();
} else {
minutes = elapsedM.toString();
}
if (elapsedS<10) {
seconds = "0"+elapsedS.toString();
} else {
seconds = elapsedS.toString();
}
if (elapsedH<10) {
hundredths = "0"+elapsedH.toString();
} else {
hundredths = elapsedH.toString();
}
_root.timer_txt = hours+":"+minutes+":"+seconds+":"+hundredths;
}
};
This function consists of 3 major parts. First we are getting the values for our hours, minutes, seconds, and hundredths. Next we are setting them up to display properly, and finally
we are outputting all the values to our text timer_txt. As I mentioned earlier getTimer() is the time in miliseconds that our movie has been open. So one hour is 3600000 miliseconds,
one minutes is 60000 miliseconds and one second is of course 1000 miliseconds. The variable remaining helps us keep track of how much extra time is left after every calculation.Copyright © 2000-2010 Spoono, LLC. All rights reserved.
Network: Reseller Web Hosting by Spoono Host | Spoonloads | Absolute Cross
Terms of Service | Privacy Policy.