Saturday, December 26, 2009

How To Create A Digital Clock In Flash

In this tutorial, you will learn how to create a digital clock using Actionscript 3.



Note: I'm using a LCD font called Digital 7 in this tutorial. You can go grab it from here.

1. Create a new flash document and set the size to 300 x 150 px and the background color to black.
2. Rename the default layer to text and create a new layer and rename it to code.



3. Create a dynamic text field on the text layer and give it an instance name of clock with the following properties:



4. Now embed the characters within the text field like shown:



5. Add the following actionscript code to the code layer:

stage.addEventListener(Event.ENTER_FRAME,updateTime);

function updateTime(e:Event):void {
var date:Date = new Date();
var hours = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();
var ampm;

// Conditional statement to set AM or PM according to hour
if (hours >= 12) {
ampm = "PM";
} else {
ampm = "AM";
}

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

if (minutes < 10){
minutes = "0" + minutes;
}

if (seconds < 10){
seconds = "0" + seconds;
}

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


6. Save your work and test the movie.

Download Source
  • rss
  • Del.icio.us
  • Digg
  • Twitter
  • StumbleUpon
  • Reddit
  • Share this on Technorati
  • Post this to Myspace
  • Share this on Blinklist
  • Submit this to DesignFloat

0 comments:

Post a Comment