Flash: Build your own professional clock

Source: Internet
Author: User
Tags getdate variables
After the clock has finished reading this article, you can make your own clock. First, we'll make a simple digital clock to show the hours and minutes, and then we'll make our project more complex. You should understand the basics of ActionScript!

In addition: The variable is English 1 but I will explain all the variables.

The author is in Flash Professional 8 do, but I think, Flash 5 should be able to do.

Let's get moving!

1. Create a new file

2. Create a dynamic text and set its variable to timetext

3. Select dynamic text, and then convert it to a movie clip (right-click it and choose Convert to Symbol/movie clip or press F8 and then select Movie clip)

4. Select the movie clip, open the Action panel, and glue the following code to the inside:

Onclipevent (enterframe) {
Time = new Date ();
hour = Time.gethours ();
minute = Time.getminutes ();
Second = Time.getseconds ();
Timetext= hour + ":" + Minute + ":" + second;
}

The first line of code onclipevent: triggers the action defined for a particular movie clip instance, Enterframe: triggers the action continuously in the frame frequency of the movie clip. Line 2nd tells the computer to create a Date object that contains the time variables we need.

Lines 3rd through 5th assign the current clock value of the system to the variable hour, assign the current minute value to the variable minute, and assign the system's current seconds value to the variable second.

Line 6th is used to display the time from the interface.

5. Save the file and test your video. If not running properly, download the example file and analyze all of your work;

6. But here it has a small bug. If the time is 23:05, the clock is displayed as 23:5. It doesn't look very beautiful. If you want to fix it, you'll have to paste a few more lines of code.

if (hour <10) {
hour = "0" + hour;
}
if (minute <10) {
minute = "0" + minute;
}
if (second <10) {
Second = "0" + second;
}

The above code means that if the clock, minutes or seconds is less than 10, then add ' 0 ' to the corresponding number.

You may not be satisfied with the results above, so let's add the year, month, day and millisecond now.

1. Edit the movie clip, add a dynamic text and set its variable to datetext;

2. Delete all the code in the movie clip and paste the following code:

Onclipevent (enterframe) {
Time = new Date ();
hour = Time.gethours ();
minute = Time.getminutes ();
Second = Time.getseconds ();
Milisecond=time.getmilliseconds ();
Year = Time.getfullyear ()//Get system years
month = Time.getmonth () +1;//get system Month
Date=time.getdate ()//Get system date
Day= Time.getday (); file://Get System Worship
if (hour <10) {
hour = "0" + hour;
}
if (minute <10) {
minute = "0" + minute;
}
if (second <10) {
Second = "0" + second;
}
if (Milisecond <10) {
Milisecond = "0" + milisecond;
}
FILE://show the week as Chinese
Switch (day) {
Case 0:
Day = "Sunday";
Break
Case 1:
Day = "Monday";
Break
Case 2:
Day = "Tuesday";
Break
Case 3:
Day = "Wednesday";
Break
Case 4:
Day = "Thursday";
Break
Case 5:
Day = "Friday";
Break
Case 6:
Day = "Saturday";
Break
}
datetext= year+ "Year" +month+ "month" +date+ "days" + day;
Timetext= hour + ":" + Minute + ":" + second+ ":" + milisecond;

}

3. Save, Test. It's working, ^^!.

4. Change the way the time or date is displayed, and edit the last 3 lines of code:

Timetext= hour + "hrs" + second + "secs" + Minute + "mins" + Milisecond;

If time is 20:15:30, your clock will appear as: 20hrs 30secs 15mins

The possible changes are infinite!

5. To prevent, save your work and then test (file/release preview/flash)

6. If it works, then you have successfully completed your professional clock!!!

To the laziest person: Add two dynamic text in Flash, set its variable to Datetext,timetext respectively. Add a new layer and paste the following code in the first frame of the action panel:
Time = new Date ();
hour = Time.gethours ();
minute = Time.getminutes ();
Second = Time.getseconds ();
Milisecond=time.getmilliseconds ();
Year = Time.getfullyear ()//Get system years
month = Time.getmonth () +1;//get system Month
Date=time.getdate ()//Get system date
Day= Time.getday (); file://Get System Worship
if (hour <10) {
hour = "0" + hour;
}
if (minute <10) {
minute = "0" + minute;
}
if (second <10) {
Second = "0" + second;
}
if (Milisecond <10) {
Milisecond = "0" + milisecond;
}
FILE://show the week as Chinese
Switch (day) {
Case 0:
Day = "Sunday";
Break
Case 1:
Day = "Monday";
Break
Case 2:
Day = "Tuesday";
Break
Case 3:
Day = "Wednesday";
Break
Case 4:
Day = "Thursday";
Break
Case 5:
Day = "Friday";
Break
Case 6:
Day = "Saturday";
Break
}
datetext= year+ "Year" +month+ "month" +date+ "days" + day;
Timetext= hour + ":" + Minute + ":" + second+ ":" + milisecond;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.