Wednesday, 2 December 2009

Display 24 hour time in Actionscript 3

In this tutorial I will show you how to display a 24 hour time in Actionscript 3.0. This method is similar to the display time in AS3 tutorial as only some small modifications are needed. The time is displayed using the Date class which gets the time and date information from the users computer. The following steps should be very familiar if you have completed the display time in AS3 tutorial.


Display 24 hour time in Actionscript 3

Step 1

Open a new Flash AS3 file. Then select the Text tool and create a dynamic text field on the stage like below:



After you have created a dynamic text field, select the ‘Character Embedding’ button and add the following glyphs: Numerals and Punctuation. You need to hold down the Ctrl key on the keyboard to select more than one glyph.


Step 2

Select your Dynamic text field and give it the following instance name as shown below:




Step 3

On the timeline insert a new layer called ‘Actions’ then open up the Actions panel and enter the following code:

//Add an event listener to the dynamic text field.
the24Time.addEventListener(Event.ENTER_FRAME,show24Time);

function show24Time(event:Event):void {
//Creates a new instances of the date class
var myTime:Date = new Date();
//This returns the hour, minutes, and seconds.
var theSeconds=myTime.getSeconds();
var theMinutes=myTime.getMinutes();
var theHours=myTime.getHours();

//This adds '0' to the hours if there is only one digit.
if (String(theHours).length == 1) {
theHours="0"+theHours;
}
//This adds '0' to the minutes if there is only one digit.
if (String(theMinutes).length == 1) {
theMinutes="0"+theMinutes;
}
//This adds '0' to the seconds if there is only one digit.
if (String(theSeconds).length == 1) {
theSeconds="0"+theSeconds;
}
//Display the 24 time in the dynamic text field.
the24Time.text =theHours+":"+theMinutes+":"+theSeconds;
}

**The 24 hour Actionsript code has basically remove nine lines of code from the display time in AS3 tutorial, so should look very similar.


Step 4

Test your 24 hour time Ctrl + Enter.



You should now have a 24 hour time in Actionscript 3.0. If you want a 12 hour time take a look at this.

0 comments:

  COPYRIGHT © 2014 · ILIKE2FLASH · Theme by Ourblogtemplates

Back to TOP