Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Making a count up clock in Actionscript 2

New Here ,
Jun 30, 2011 Jun 30, 2011

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?!

TOPICS
ActionScript
3.3K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Jun 30, 2011 Jun 30, 2011

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

...
Translate
LEGEND ,
Jun 30, 2011 Jun 30, 2011

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 30, 2011 Jun 30, 2011

I'm sorry can you elaborate more? I found some stuff on the getTimer like you suggested but I'm terrible w/ actionscript.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 30, 2011 Jun 30, 2011

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 30, 2011 Jun 30, 2011

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 01, 2011 Jul 01, 2011

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"?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 01, 2011 Jul 01, 2011

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 01, 2011 Jul 01, 2011

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);

   }

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 01, 2011 Jul 01, 2011
LATEST

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines