|
||||||
|
||||||
Flash Tutorial - Animated Clock

Free
Tutorial
![]()
This Flash tutorial shows you how to make an animated clock that follows the mouse as your cursor moves. The clock gives the viewer the time and date based on the settings on their local computer. So no matter where they are in the world the clock will be correct for them, as long as their computer clock is correct. The clock is built with ActionScript so the download time is very small.
Example: Download
the Flash file Int
134a Flash MX
Roll over the Movie to see how the clock animates
and follows the mouse.
Set One: Setting up the Flash Movie
Note: This letter X does not display, it only makes the text box easier to find and edit.



Note: In the Library you should now have two symbols. If you wish to check open your Library: Window > Library (F11)
The Symbols in my Library.
Step Two: The ActionScript MC Frame 1
Note: If you have any questions about the code and how it works feel free to ask on the webwasp forum.
// If the following has not loaded then run the code and set the loaded variable to true so that it wont load again
// Basically only run this code once
// This code basically sets up the properties and components that in the next frame we will use to build the clock
if (!_load) {
// Set the variable loaded to 1 to show the code has now been loaded
_load = 1;
// Set the target to this
tellTarget ("..") {
// Set the diameter of the clock face
clockDiameter = 40;
// Set the vertical off set distance from the mouse
clockOffsetY = 0;
// Set the horizontal off set distance from the mouse
clockOffsetX = 100;
// Set the update speed
easingRate = 0.750000;
// Set the date rotation speed
dateRotationStep = 0.100000;
// Set the rotation starting point
dateRotation = 0;
// Set the height of the hands of the clock
HandHeight = clockDiameter/4.500000;
// Set the width of the clock hands
HandWidth = clockDiameter/4.500000;
// Set up the days of the week
var dayNames = ["SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY"];
// Set up the months of the year
var monthNames = ["JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE", "JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER"];
// Set up a variable and set its value to the system date
var now = new Date();
// Get the day of the week from the variable
var day = now.getDate();
// Get the year value from the variable
var year = now.getYear();
// If the date is less than the year 2000 compensate for this
if (year<2000) {
year = year+1900;
}
// Set a variable to today's date : today's day name +today's months name + this year
// Example: Tuesday March 2009
var todaysDate = " "+dayNames[now.getDay()]+" "+day+" "+monthNames[now.getMonth()]+" "+year;
// Remove any spaces form the date value taken from the variable and break the date up into this format:
// T
// u
// s
// d
// a
// y
// M
// a
// r
// c
// h
// 2
// 0
// 0
// 9
D = todaysDate.split("");
// Set a variable H to ...
H = "...";
// Remove spaces from each word to return:
// .
// .
// .
H = H.split("");
// Set a variable M to ....
M = "....";
// Remove spaces from each word to return:
// .
// .
// .
// .
M = M.split("");
// Set a variable S to .....
S = ".....";
// Remove spaces from each word to return:
// .
// .
// .
// .
// .
S = S.split("");
// Set a variable F to 12 1 2 3 4 5 6 7 8 9 10 11
F = "12 1 2 3 4 5 6 7 8 9 10 11";
// Remove spaces from each word to return:
// 12
// 1
// 2
// 3
// 4
// 5
// 6
// 7
// 8
// 9
// 10
// 11
F = F.split(" ");
// Set up the clock face angle
FaceDeltaAngle = 360/F.length;
// Set up the date angle
DateDeltaAngle = 360/D.length;
// Set the hands X and Y center location
HandY = 0;
HandX = 0;
// Create an array for the numbers on the clock
yHand = new Array();
xHand = new Array();
// For the length of the variable F set the numbers "12 1 2 3 4 5 6 7 8 9 10 11" into an array
for (i=0; i<F.length; i=i+1) {
yHand[i] = 0;
xHand[i] = 0;
}
// Create an array for the date
yDate = new Array();
xDate = new Array();
// For the length of the variable date set the value of the date into an array
for (i=0; i<D.length; i=i+1) {
yDate[i] = 0;
xDate[i] = 0;
}
// Set a variable to 1
j = 1;
// Create a movie clip and add each character from the variable D
for (i=0; i<D.length; i=i+1) {
ch = myLetterX.duplicateMovieClip("chDate"+i, j);
j = j+1;
ch.mytext = D[i];
}
// Create a movie clip and add each character from the variable F
for (i=0; i<F.length; i=i+1) {
ch = myLetterX.duplicateMovieClip("chFace"+i, j);
j = j+1;
ch.mytext = F[i];
}
// Create a movie clip and add each character from the variable S
for (i=0; i<S.length; i=i+1) {
ch = myLetterX.duplicateMovieClip("chSeconds"+i, j);
j = j+1;
ch.mytext = S[i];
}
// Create a movie clip and add each character from the variable M
for (i=0; i<M.length; i=i+1) {
ch = myLetterX.duplicateMovieClip("chMinutes"+i, j);
j = j+1;
ch.mytext = M[i];
}
// Create a movie clip and add each character from the variable H
for (i=0; i<H.length; i=i+1) {
ch = myLetterX.duplicateMovieClip("chHours"+i, j);
j = j+1;
ch.mytext = H[i];
}
// Hide the myLetterX movie clip
myLetterX._visible = false;
}
// End of TellTarget
}
// Go to the next frame
nextFrame();
play();
Step Three: The ActionScript MC Frame 3
Note: You should still be Editing inside the Movie Clip: ActionScript MC
Note: If you wanted the animation to play at a slower speed you could place the code on a frame further down the timeline, such as Frame 10 etc.
// Set the target to this
tellTarget ("..") {
// Get the location of the mouse and set the clock location accordingly
yClock = _ymouse+clockOffsetY;
xClock = _xmouse+clockOffsetX;
// Set the date location
yDate[0] = yDate[0]+(yClock-yDate[0])*easingRate;
xDate[0] = xDate[0]+(xClock-xDate[0])*easingRate;
// For the length of the variable D set the date center starting location and easing rate
for (i=1; i<D.length; i=i+1) {
yDate[i] = yDate[i]+(yDate[i-1]-yDate[i])*easingRate;
xDate[i] = xDate[i]+(xDate[i-1]-xDate[i])*easingRate;
}
// Set the hands of the clock center starting location and easing rate
yHand[0] = yHand[0]+(yClock-yHand[0])*easingRate;
xHand[0] = xHand[0]+(xClock-xHand[0])*easingRate;
// For the length of the variable F set the hands center starting location and easing rate
for (i=1; i<F.length; i=i+1) {
yHand[i] = yHand[i]+(yHand[i-1]-yHand[i])*easingRate;
xHand[i] = xHand[i]+(xHand[i-1]-xHand[i])*easingRate;
}
// Get the system date
var time = new Date();
// Get the seconds from the system date
var seconds = time.getSeconds();
// Get the minutes from the system date
var minutes = time.getMinutes();
// Get the hours from the system date
var hours = time.getHours();
// Set the angle for the seconds
var secondsAngle = 3.141593*2*(seconds/60-1.250000);
// Set the angle for the minutes
var minutesAngle = 3.141593*2*((minutes+seconds/60)/60-1.250000);
// Set the angle for the hours
var hoursAngle = 3.141593*2*((hours+minutes/60)/12-1.250000);
// Set up the clock face numbers with some maths
for (i=0; i<F.length; i=i+1) {
var faceSprite = eval("chFace"+i);
a = (i*FaceDeltaAngle-90)*3.141593/180;
faceSprite._y = yHand[i]+clockDiameter*Math.sin(a);
faceSprite._x = xHand[i]+clockDiameter*Math.cos(a);
}
// Set the location for the hour hand based on some maths
for (i=0; i<H.length; i=i+1) {
var hoursSprite = eval("chHours"+i);
hoursSprite._y = yHand[i]+HandY+i*HandHeight*Math.sin(hoursAngle);
hoursSprite._x = xHand[i]+HandX+i*HandWidth*Math.cos(hoursAngle);
}
// Set the location for the minute hand based on some maths
for (i=0; i<M.length; i=i+1) {
var minutesSprite = eval("chMinutes"+i);
minutesSprite._y = yHand[i]+HandY+i*HandHeight*Math.sin(minutesAngle);
minutesSprite._x = xHand[i]+HandX+i*HandWidth*Math.cos(minutesAngle);
}
// Set the location for the second hand based on some maths
for (i=0; i<S.length; i=i+1) {
var secondsSprite = eval("chSeconds"+i);
secondsSprite._y = yHand[i]+HandY+i*HandHeight*Math.sin(secondsAngle);
secondsSprite._x = xHand[i]+HandX+i*HandWidth*Math.cos(secondsAngle);
}
// Set the circular rotation of the date around the clock face with some maths
for (i=0; i<D.length; i=i+1) {
var dateSprite = eval("chDate"+i);
dateSprite._y = yDate[i]+clockDiameter*1.500000*Math.sin(dateRotation+i*DateDeltaAngle*3.141593/180);
dateSprite._x = xDate[i]+clockDiameter*1.500000*Math.cos(dateRotation+i*DateDeltaAngle*3.141593/180);
}
// Set the date rotation speed
dateRotation = dateRotation-dateRotationStep;
}
// Replay the code and update the code to show the new time from last time it was checked
gotoAndPlay(2);
Note: the ActionScript is complete but it needs to be placed inside the Clock Movie Clip.
Note: You should find your Letter X Movie Clip inside the Clock MC.
That's it! Easy Hey?
Why not try out webwasp's new community. Meet new people, find friends in your area: Webwasp Mates & Dates
•
43930 visitors to this page since
Jan 05•
|
|
|

Webwasp is Phil Schulz's baby. You are welcome to contact me or become my Facebook friend:
Click here
All material on this site is protected under international copyright © law. DO NOT reproduce any material from this site without written permission. Please ask as permission is often granted.
|