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.