Timer class--timer and clock example in AS3

Source: Internet
Author: User

Example 1:
No example is more suitable than a clock to describe the basic application of a timer class.
Step One:
Place a clock face on the flash stage, and then create a new layer to draw the hour hand, minute hand, second hand, and axis. 3 pointers, respectively, are named

Hourpoint,minutepoint,secondpoint. The registration points of these pointers are at the bottom of the center. As shown in figure:

Now let's get it going:
Enter clock in the Properties panel. Creates a new document class clock.as. The code is as follows:
Code:

The code is as follows Copy Code
[Code]
Package
{
Import Flash.display.Sprite;
Import Flash.display.MovieClip;
Import Flash.utils.Timer;
Import flash.events.TimerEvent;
public class Clock extends Sprite
{
var clocktimer:timer=new Timer (1000,0);
var bellsound:bell=new Bell ();
Public Function Clock ()
{
Refreshpoint (); Initializing pointers
Clocktimer.start (); Start Running timer
Clocktimer.addeventlistener (timerevent.timer,timerfunction); Adding events
}

Private Function Timerfunction (evtobj:timerevent): void
{
Refreshpoint ();
}

Private Function Refreshpoint (): void
{
var currentdate:date=new Date ();
The second hand rotates 6 degrees each way.
secondpoint.rotation=currentdate.seconds*6;
The minute hand is rotated 6 degrees plus seconds for each increment
MINUTEPOINT.ROTATION=CURRENTDATE.MINUTES*6+CURRENTDATE.SECONDS*6/60;
Use the remainder method to convert the 24-hour system to a 12-hour system, with the hour hand rotated 30 degrees each way and the increment for it.
var hour12:int=currentdate.hours%12;
HOURPOINT.ROTATION=HOUR12*30+CURRENTDATE.MINUTES*30/60;

Hour timekeeping function
if (currentdate.minutes==0 && currentdate.seconds==0)
{
if (hour12)
{
Bellsound.play (0,HOUR12);
}
Else
{
Bellsound.play (0,12); Knock 12 at 0 o'clock.
}
}
}
}
}

The above code has two points to clarify:
The date class is 24 hours, and the clock is 12 hours, and the method of conversion is to take the remainder. Currentdate.hours%12.
Two, 0 points represent 12 points. When you reach 0 o'clock, knock 12 down instead of 0.

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.