Monday, 14 September 2009

Display time in Actionscript 3

In this tutorial you will learn how to display the time in Actionscript 3.0. This is an update from the previous AS2 version. The code in this tutorial is very similar to the previous version, only minor adjustments are needed. Displaying the time uses the Date class which represents the time and date information. I have created this tutorial in Flash CS4, but it should work fine in CS3.

Display time in Actionscript 3

Step 1

Open a new Flash AS3 file.
Select the text tool with dynamic text and drag a dynamic text field on the stage as shown below:

Select the dynamic text field then choose the ‘Character Embedding’ and select Lowercase, Numerals and Punctuation.

Step 2

Give your dynamic text field the instance name ‘theTime’ like below:

Step 3

On the timeline create a new layer called ‘Actions’. Then select the first frame and hit F9 to open up the actions panel and enter the following code:

//Adds the enter frame event to the dynamic text field.

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

//Displays am/pm depending on the current hour.
if (theHours>=12) {
} else {
//This subtracts 12 from the hour when it greater than 13.
if (theHours>=13) {
//Adds '0' if there is only one digit.
if (String(theMinutes).length == 1) {
if (String(theSeconds).length == 1) {
//Displays the time in the dynamic text field.
theTime.text =theHours+":"+theMinutes+":"+theSeconds+" "+ampm;

Step 4

Test the movie clip Ctrl + Enter. You should now have a time display in Actionscript 3.0.


Joe Rucci 1 December 2009 at 14:08  


theMinutes == 1
theSeconds == 1

should be...

theMinutes < 10
theSeconds < 10

iliketo 2 December 2009 at 04:30  


Code is updated.

tracy 28 April 2010 at 01:36  

**Error** Scene=Scene 1, layer=Actions, frame=1:Line 4: The class 'Event' could not be loaded.
function showTime(event:Event):void {

Total ActionScript Errors: 1 Reported Errors: 1
an error appear what should i do:(??

iliketo 28 April 2010 at 12:02  


Are you using an Actionscript 3 file?

  COPYRIGHT © 2014 · ILIKE2FLASH · Theme by Ourblogtemplates

Back to TOP