Create an Attractive Digital Clock in Flash

ActionScript 3.0 is a very powerful language, capable of creating anything from simple and light utilities to full-featured desktop applications.

In this tut, we’re going to create something simple, a basic and good looking digital clock in Flash.


Step 1: Brief Overview

Using the Date object and its properties, we’ll get the day, hours, minutes and seconds and use TextFields on stage to display the obtained data. The updates will be handled by a Timer.


Step 2: Starting

Open Flash and create a new Flash File (ActionScript 3).

flash clock

Set the stage size to 600×300 and add a blue to black radial background (#003030, #000000).

”flash

Step 3: Glowing Text

We’re going to add two Dynamic Textfields for every element, we’ll place the ones with the glow first to get the lines effect.

Select the Text Tool and create a 342x104px textfield, use the color #00FAFF, click the Center option in the Paragraph panel and choose a font you like. I used DS-Digital italic, 100pt.

Name it "clockGlow", add some numbers, center it to use it as a guide and add a Glow Filter with the following values:

flash clock

You should have something like this:

flash clock

Repeat the process with a smaller font size for the days and the am/pm indicator. The instance names are the name of the day plus the "Glow" word, this will be "monGlow", "tueGlow" and so on, "ampmGlow" for the AM/PM indicator.

flash clock

Step 4: Lines Screen

We’ll draw a series of lines that will cover the stage to get the LCD screen effect.

Select the Rectangle Tool and create a 600x1px black line, duplicate it (Cmd+D) and place it below leaving a 1px space.

Repeat the process until it matches the stage height. You will end with something like this:

flash clock

Convert the lines screen to a group (Cmd+G) and center it in the stage.

flash clock

Step 5: Sharp Text

As you can see in the last image, the line effect is all over the text. We only want it to apply to the glow, so let’s add a new layer of text.

As this text is exactly the same we created before (without the Glow Filter) we can just copy the textfields and remove the Glow Filter. We’ll also remove the "Glow" word from the instance names.

This will complete the interface.

flash clock

Step 6: ActionScript Time

Create a new ActionScript Document and save it as "Main.as".

flash clock

Step 7: Required Classes

This time we’ll need just a few classes.

package 
{
	import flash.display.MovieClip;
	import flash.utils.Timer;
	import flash.events.TimerEvent;

Step 8: Extending the Class

We’re going to use MovieClip specific methods and properties so we extend using the MovieClip Class. Extending using the Sprite Class won’t work.

public class Main extends MovieClip
{

Step 9: Variables

These are the variables we will use, explained in the comments.

/* A Date object used to get the day, and time */

var date:Date = new Date();

var day:int = date.day; //The day

/* The time */

var hours:int = date.hours;
var minutes:* = date.minutes;
var seconds:* = date.seconds;

/* A Timer object that will handle the updates, executed every second */

var timer:Timer = new Timer(1000);

Step 10: Main Function

This function is executed when the class is loaded.

public function Main():void
{
	/* Prevents lag, since the timer is not executed until 1 second after starting the movie */

	updateClock();
			
	/* Hides all days text, these functions are explained later */

	hideObjects(mon, monGlow, tue, tueGlow, wed, wedGlow, thu, thuGlow, fri, friGlow, sat, satGlow, sun, sunGlow);
			
	/* Unhides the current day */
			
	showCurrentDay();
			
	/* Starts the timer */

	timer.addEventListener(TimerEvent.TIMER, startClock);
	timer.start();
}

Step 11: Clock Function

This is the function that handles the Clock. It is called once in the main function, then every second in the startClock function.

private function updateClock():void
{
	/* AM PM, if hours is greater than 11, that is 12 and 12 is PM */

	if (hours>11)
	{
		ampm.text="PM";
		ampmGlow.text="PM";
	}
	else
	{
		ampm.text="AM";
		ampmGlow.text="AM";
	}

	/* Avoids 24 hour clock, if hours is greater than 12 (like 13) substracts 12 (so it is 1) */

	if (hours>12)
	{
		hours-=12;
	}

	/* If number is just one digit, add a 0 to the left */

	if (String(minutes).length<2)
	{
		minutes="0"+minutes;
	}

	if (String(seconds).length<2)
	{
		seconds="0"+seconds;
	}

	/* Set TextFields */

	clock.text=hours+":"+minutes+":"+seconds;
	clockGlow.text=hours+":"+minutes+":"+seconds;
}

Step 12: Hide Function

This is a function to make objects invisible, we use the …rest parameter to pass any number of parameters.

private function hideObjects(...targets)
{
	for (var i:int = 0; i < targets.length; i++)
	{
		targets[i].visible=false;
	}
}

Step 13: Current Day

This Switch statement will check the day variable to get the day and unhide it.

private function showCurrentDay():void
{
	switch (day)
	{
		case 0 :
			sun.visible=true;
			sunGlow.visible=true;
			break;
		case 1 :
			mon.visible=true;
			monGlow.visible=true;
			break;
		case 2 :
			tue.visible=true;
			tueGlow.visible=true;
			break;
		case 3 :
			wed.visible=true;
			wedGlow.visible=true;
			break;
		case 4 :
			thu.visible=true;
			thuGlow.visible=true;
			break;
		case 5 :
			fri.visible=true;
			friGlow.visible=true;
			break;
		case 6 :
			sat.visible=true;
			satGlow.visible=true;
			break;
		default :
			trace("Week day error");
	}
}       

Step 14: The Start Function

This function updates the Date object to get the actual date and calls the updateClock function to update the textfields.

private function startClock(e:TimerEvent):void
{
	date = new Date();

	hours=date.hours;
	minutes=date.minutes;
	seconds=date.seconds;
			
	updateClock();
}

Step 15: Document Class

Go back to the .Fla file and in the Properties Panel add "Main" in the Class field to make this the Document Class.

flash clock

Conclusion

As you can see, it’s pretty easy to create and modify this Digital Clock. Experiment with the Date Class, change the interface and maybe add more functionality!

Thanks for reading.

  • Jaysyn

    anyone know why my published swf font isnt italicised when i run it? it is in the fla file but the swf file it is not. HELP??

  • KJ

    I’m using CS3 and I need to add 1 line to step 7:

    import flash.text.TextField;

    Anyway, great tutorial. Thanks.

  • JeffB

    I’m getting lots of errors in the actionscript.
    Is there anyway to just copy and paste this in so the errors do not come up?

  • JeffB

    Nevermind, some how I missed that giant Source Button… Perhaps more sleep is needed…

  • flui

    It’s very cool.
    It’s really like a digital clock.

  • http://flash-clocks.com Flash Clocks

    I am going to make a similar digital flash clock as yours here, i never used that color nor that glow filter which is the main attraction in my opinion, of course the ds-digital font is great but for it to work visually smoothly, i think i should aligh the text at left, i think you have it aligned at center, that is why i suppose the change/ship of numbers make the clock move, so in that matter i advice you to update the digital flash clock, if you want of course.

  • http://priostudio.daportfolio.com Prionkor

    I get these errors:

    1046: description Type was not found or was not a compile-time constant: TextField. source package{

    this error is in every line including import statements.

    Please help me out someone!

  • http://flash-clocks.com Flash Clocks

    Check the actionscript source code you entered again, that seems like an error on the source code somewhere, however i am not sure, if you downloaded the as file, unless you tried to insert it manually yourself.

  • metal

    Does this clock can be used as screensavers

  • metal

    with what programs can do the lesson and where can I download

  • morgan

    hi carlos,
    loved the clock. but i had a little trouble. i need a background to match my site and a different font and the clock just wont work now. i made the background cement #dedcd5 and chose the helvetica font and errors are everywhere. any ideas buddy?

    btw, i’m not experienced at all with flash but i know i only made those changes.

  • http://www.eduardoribeiro.pt.vu Eduardo

    Hi!

    I have a seriously problem. I made this tutorial and everything works very well, but I try integrate this clock in another project. The question is: Why I can’t import the .as file to .fla without be like the step 14? The way I need to import .as files is in the actions panel with this code ” import package.*; ” (import all as files in the package)

  • Zlatko

    Hi buddy I have the same error 1046, this is a compiler error and it is shown I guess in older versions because I use CS3. I checked the code a few times…. there was no errors and the compiler editor shows errors from the first line!
    It looks something like this……

    Main.as, Line 1 1046: Type was not found or was not a compile-time constant: TextField.
    Main.as, Line 3 1046: Type was not found or was not a compile-time constant: TextField.
    Main.as, Line 4 1046: Type was not found or was not a compile-time constant: TextField.
    Main.as, Line 4 1046: Type was not found or was not a compile-time constant: TextField.
    Main.as, Line 5 1046: Type was not found or was not a compile-time constant: TextField.
    Main.as, Line 7 1046: Type was not found or was not a compile-time constant: TextField.
    Main.as, Line 7 1046: Type was not found or was not a compile-time constant: TextField.
    Main.as, Line 9 1046: Type was not found or was not a compile-time constant: TextField.
    Main.as, Line 10 1046: Type was not found or was not a compile-time constant: TextField.
    Main.as, Line 10 1046: Type was not found or was not a compile-time constant: TextField.
    Main.as, Line 11 1046: Type was not found or was not a compile-time constant: TextField.
    Main.as, Line 12 1046: Type was not found or was not a compile-time constant: TextField.
    Main.as, Line 12 1046: Type was not found or was not a compile-time constant: TextField.
    Main.as, Line 13 1046: Type was not found or was not a compile-time constant: TextField.
    Main.as, Line 14 1046: Type was not found or was not a compile-time constant: TextField.
    Main.as, Line 14 1046: Type was not found or was not a compile-time constant: TextField.
    Main.as, Line 16 1046: Type was not found or was not a compile-time constant: TextField.
    Main.as, Line 16 1046: Type was not found or was not a compile-time constant: TextField.

  • Chud37

    Hello Everyone,

    If you get the errors “Type was not found or was not a compile-time constant: Textfield” Then its because you are using a newer version of flash than this tutorial was made for.

    Im using CS3, and only one person on this page has made the correct statement, you need to import the textfield mumbo-jumbo. So the first few lines should be:

    package
    {
    import flash.text.TextField;
    import flash.display.MovieClip;
    import flash.utils.Timer;
    import flash.events.TimerEvent;

    Just adding the import.flash.text.TextField; into there.

    Even so, The clock displays properly but on mine it did not move. I cant be bothered at this moment to work out why; I dont like just copying other peoples code really but this was a nice looking clock. Well done to the guy who made it.

    ~Chud37

  • daniel

    hola a todos… bueno la verda el diseño y todo esta muy chevere pero tengo una duda como hago para insertar este maravilloso reloj en wampserver o las varible que se necesita para montarlo en una pagina web

  • http://translate.google.com.co/translate?hl=es&sl=en&tl=es&u=http%3A%2F%2Factive.tutsplus.com%2Ftutorials%2Factionscript%2Fcreate-an-attractive-digital-clock-in-flash%2F&anno=2 dkrraskl

    hello everyone …

    I’m good to start new at this and need your help because you are a hard on this, the question is how do I raise this watch for one to a web page, or rather to insert it in a database I need make a database that captures me and entered the arrival of teachers who are teaching me and I liked this watch for that database … can help me it is urgent hagradesco heart to the person I want ayuar …

  • Pingback: Porting an Application to iPhone Using Flash CS5 - GFX Me

  • Max

    nice desing but the code is wrong.. I got 17 errors

  • Haito51

    I got the code to work just fine, i went thru and hand coded everything, in the above copy there is (<) just need to change it to <, there are a few smaller gotcha's but easy to manage, great tutorial, I will be sure to give you credit on a project idea I am working on thx

  • Haito51

    I got the code to work just fine, i went thru and hand coded everything, in the above copy there is (&lt) just need to change it to <, there are a few smaller gotcha's but easy to manage, great tutorial, I will be sure to give you credit on a project idea I am working on thx

  • Pingback: Porting an Application to iPhone Using Flash CS5

  • Lyon

    Hi, how do i use the getUTChours in the Main class Carlos, i want to retrieve the utc hours and perhaps add an utc offset number.

  • Robert

    I’d like to see the code in it’s entirety, i’m new to code and seeing snippits of it doesn’t help me

  • Cong

    If you want to have 1 and thin numbers to stay where they are, you can substitute with Digital Readout Thick Upright font from http://www.fontstock.net/715/digital-readout-thick-upright.html.

  • Cong

    If you want to preserve the font you are using and still get the real digital effect, you can use the rolling counter for pinball game approach from ActiveTuts+ (http://active.tutsplus.com/tutorials/effects/create-a-pinball-style-rolling-score-counter-class/)

    This works out nicely for every fonts. I include a zip file in the link at the bottom containing the modified FLA, AS for CS5, and also a screensaver made from the SWF.

    You can read a tutorial on making screensavers from SWF also on Activetuts+ on (http://active.tutsplus.com/tutorials/actionscript/flash-clock-analo/). The only difference was that I used InstantStorm (http://download.cnet.com/InstantStorm/3000-2409_4-10610080.html), which was free instead of the $200 Screentime like Carlos used. So all-rounder ActiveTuts! Thanks guys!

    FLA and AS: http://www.mediafire.com/file/z4ckd12l145d8ka/DigitalClock.zip

    • http://michaeljameswilliams.com/ Michael James Williams

      Great links, thanks Cong!

  • Pingback: ashlingon

  • Peckles

    You might want to consider adding

    if(hours == 0) hours == 12;

    :P