Thursday 10 September 2009

Create an Attractive 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).

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

””

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:

You should have something like this:

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.

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:

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

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.

Step 6: ActionScript Time

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

Step 7: Required Classes

This time we'll need just a few classes.

  1. package
  2. {
  3. import flash.display.MovieClip;
  4. import flash.utils.Timer;
  5. 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.

  1. public class Main extends MovieClip
  2. {

Step 9: Variables

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

  1. /* A Date object used to get the day, and time */
  2. var date:Date = new Date();
  3. var day:int = date.day; //The day
  4. /* The time */
  5. var hours:int = date.hours;
  6. var minutes:* = date.minutes;
  7. var seconds:* = date.seconds;
  8. /* A Timer object that will handle the updates, executed every second */
  9. var timer:Timer = new Timer(1000);

Step 10: Main Function

This function is executed when the class is loaded.

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

  5. /* Hides all days text, these functions are explained later */
  6. hideObjects(mon, monGlow, tue, tueGlow, wed, wedGlow, thu, thuGlow, fri, friGlow, sat, satGlow, sun, sunGlow);

  7. /* Unhides the current day */

  8. showCurrentDay();

  9. /* Starts the timer */
  10. timer.addEventListener(TimerEvent.TIMER, startClock);
  11. timer.start();
  12. }

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.

  1. private function updateClock():void
  2. {
  3. /* AM PM, if hours is greater than 11, that is 12 and 12 is PM */
  4. if (hours>11)
  5. {
  6. ampm.text="PM";
  7. ampmGlow.text="PM";
  8. }
  9. else
  10. {
  11. ampm.text="AM";
  12. ampmGlow.text="AM";
  13. }
  14. /* Avoids 24 hour clock, if hours is greater than 12 (like 13) substracts 12 (so it is 1) */
  15. if (hours>12)
  16. {
  17. hours-=12;
  18. }
  19. /* If number is just one digit, add a 0 to the left */
  20. if (String(minutes).length<2)>
  21. {
  22. minutes="0"+minutes;
  23. }
  24. if (String(seconds).length<2)>
  25. {
  26. seconds="0"+seconds;
  27. }
  28. /* Set TextFields */
  29. clock.text=hours+":"+minutes+":"+seconds;
  30. clockGlow.text=hours+":"+minutes+":"+seconds;
  31. }

0 nhận xét:

Post a Comment

 

Blogroll

Site Info

Text

Tut - Designer Copyright © 2009 WoodMag is Designed by Ipietoon for Free Blogger Template