Copy link to clipboard
Copied
I need to make a clock that starts at 10:00:00 (showing 10 hours, minutes, seconds).
It needs to count up to 12:00:00 (in real time) and if it hits 12:00:00 then it starts over back at 10:00:00
Any help please?!
1 Correct answer
Here's a basic example of what I was describing. If you place a dynamic textfield on the stage and give it an instance name of "tField", then place this code in the timeline, you will see it counting up seconds.
var startMSec = getTimer(); // get the start time for the first cycle
this.onEnterFrame = function(){
var currentMSec = getTimer(); // get the current time that has passed
var msecsSoFar = currentMSec - startMSec; // calculate the time difference
var secsSoFar = Math.floor(ms
...Copy link to clipboard
Copied
Look into using the getTimer() function. It returns the number of milliseconds that have elapsed since the swf started playing. You can use it to keep track of the how much time has passed from a given starting point, which you establish using it, resetting the starting point each time 2 hours passes.
So you just take that milliseconds difference between the starting getTimer value and the current getTimer value and calculate the hours,minutes, and seconds it represents and add it to the 10 hours you start with.
You will need to continually call the function to keep the time updating, which you can do using an onEnterFrame scheme.
Copy link to clipboard
Copied
I'm sorry can you elaborate more? I found some stuff on the getTimer like you suggested but I'm terrible w/ actionscript.
Copy link to clipboard
Copied
What aspect of it do you not understand? What I offerred is basically all you need to do. Take a current time and subtract a starting time and figure out how much time that represents in hours, minutes, and seconds... repeat as needed.
Copy link to clipboard
Copied
Here's a basic example of what I was describing. If you place a dynamic textfield on the stage and give it an instance name of "tField", then place this code in the timeline, you will see it counting up seconds.
var startMSec = getTimer(); // get the start time for the first cycle
this.onEnterFrame = function(){
var currentMSec = getTimer(); // get the current time that has passed
var msecsSoFar = currentMSec - startMSec; // calculate the time difference
var secsSoFar = Math.floor(msecsSoFar/1000); // convert to seconds
tField.text = String(secsSoFar); // display the seconds in the textfield
}
For what you want to create you will need to add in more stuff in the function, such as conditionals to test for your time limit and other code to determine the hours, minutes, and seconds you need to display.
Copy link to clipboard
Copied
Okay, thanks for your help.
Right now I have 3 dynamic text fields that count up the secs/mins/hrs... they work separately so I need to figure out how to make them start over once they reach a certain point (so at second 60 in seconds for example). Is that the best way to do it, or is there some way to make the 3 of them recognize each other?
Here's what I have so far...
var startMSec = getTimer(); // get the start time for the first cycle
this.onEnterFrame = function(){
var currentMSec = getTimer(); // get the current time that has passed
var msecsSoFar = currentMSec - startMSec; // calculate the time difference
var secsSoFar = Math.floor(msecsSoFar/1000); // convert to seconds
var minsSoFar = Math.floor(msecsSoFar/60000); // convert to mins
var hrsSoFar = Math.floor(msecsSoFar/3600000) + 10; // convert to hours
tField_secs.text = String(secsSoFar); // display the seconds in the textfield
tField_mins.text = String(minsSoFar); // display the mins in the textfield
tField_hours.text = String(hrsSoFar) ; // display the hours in the textfield
}
Also, is there a way to make it start out as "00" and count up "00...01...02...03" instead of "1...2...3...4"?
Copy link to clipboard
Copied
What you'll want to do is test for the time value to determine when you need to set a new start time. You could do that either by checking the secsSoFar to determine when it has reached two hours worth of seconds, or you could check the textfields to see if they match the 12:00:00 value you're waiting for, or you could find any of a couple other means of determining when the two hours is up.
As far as the display goes, what is normally done is you test if the value is < 10 and if so you add a "0" character in front of it.
Copy link to clipboard
Copied
I've been playing around with if statements because I assumed that's what I need but I'm not having any luck. This is what I have (for the seconds counter)...
if(secSoFar > 59) {
set(tField_secs, 0);
}
Copy link to clipboard
Copied
You'll need to think a little more regarding how you make use of the time value you acquire. The secsSoFar number is the total seconds, which will exceed 59 for most of the two hours it is building up. The seconds you end up displaying have to be determined via what's left over after the number of hours and minutes it contains are factored out.

