Analog Clock E.J. aka IamNotJubba This tutorial is designed to help you create an analog clock with a sweeping second hand:
Before we start, Click here to download the partial source. The partial source does not contain the important actionscript, only the images you will need to make this tutorial easier to follow. There is not too much actionscript involved with this so it is not too difficult an effect to duplicate. I'll also explain each bit of code to make it more understandable. The code below is the code you will need to copy and paste into the final movie clip at the end of the tutorial.
Let's begin:
onClipEvent (enterFrame) This bit of code loops the actions contained within it everytime the movie is accessed. time = new Date(); This creates a date object, and allows you execute ther next commands. mil = time.getMilliseconds(); s = time.getSeconds(); m = time.getMinutes(); h = time.getHours(); This part of the code gets all the information from your system clock, and incorporates it into Flash. seconds._rotation = s*6+(mil/(1000/6)); Since there are 60 seconds in a minute, and we want the seconds hand to rotate a full 360°, we mutiply the 60 seconds by 6. (60 x 6 = 360) Now, the milliseconds are there to create the constant sweep. "The milliseconds represent 6 degrees of rotation (one second), therefore you take the current milliseconds and divide it by total possible milliseconds (1000) to get a fraction, then you divide that fraction by the 6 degrees to get the milliseconds' rotation." --thanks to Suprabeener for this explaination minutes._rotation = m*6+(s/6); Again, since there are 60 minutes in an hour, and we want the minute hand to rotate a full 360°, we multiply the 60 minutes by 6. (60 x 6 = 360) The seconds are there to help create a continuous sweep. Every minute is 6° of rotation and every second is 6° of rotation, so with every second that goes by, this code adds one degree to the minute hand. hours._rotation = h*30+(m/2); Now, there are 12 hours in a full rotation, so we multiply hours by 30. (12 x 30 = 360) Each mark for hours is a full 30° so in order to get a sweeping motion, we want to add one degree for every two minutes that pass, and thats wut the "+(m/2)" part of the code does. That's it. I hope this helped some people, and thanks to all the people that helped me with this. |