User Guide
Example: Simple analog clock 207
This method saves the current time in a variable, so the time can’t change in the middle of
drawing the clock hands. Then it calls the
showTime() method to display the hands, as the
following shows:
/**
* Displays the given Date/Time in that good old analog clock style.
*/
public function showTime(time:Date):void
{
// gets the time values
var seconds:uint = time.getSeconds();
var minutes:uint = time.getMinutes();
var hours:uint = time.getHours();
// multiplies by 6 to get degrees
this.secondHand.rotation = 180 + (seconds * 6);
this.minuteHand.rotation = 180 + (minutes * 6);
// Multiply by 30 to get basic degrees, then
// add up to 29.5 degrees (59 * 0.5)
// to account for the minutes.
this.hourHand.rotation = 180 + (hours * 30) + (minutes * 0.5);
}
First, this method extracts the values for the hours, minutes, and seconds of the current time.
Then it uses these values to calculate the angle for each hand. Since the second hand makes a
full rotation in 60 seconds, it rotates 6 degrees each second (360/60). The minute hand
rotates the same amount each minute.
The hour hand updates every minute, too, so it can show some progress as the minutes tick
by. It rotates 30 degrees each hour (360/12), but it also rotates half a degree each minute (30
degrees divided by 60 minutes).