Example:
In high school I took computer science (java based) and loved it. I don't know if it was the teacher I had or the fact that you can make things with lines of code, but whatever the reason I was really excited when I was introduced to flash, a program that uses the source code actionscript which is very similar to java. I just finished watching a tutorial on how to make a clock in flash using actionship and my results were a fully functional clock(aka. it can keep time) with hour, minute, and seconds hands. Below is the code used to create the clock.If you were to insert this into the actions in flash your output would be a blue clock with hour and minute ticks and hour, minute, and second hands that rotate according to the time.
//creates existence of clock
this.createEmptyMovieClip("clock", 3);
clock._x = 150;
clock._y = 150;
//blue circle
clock.createEmptyMovieClip("circle", 1);
clock.circle.lineStyle(4, 0x000066, 100);
clock.circle.moveTo(100, 0);
circleRadius = 100;
for (a=0; a<361;>
{
radAngle = a*Math.PI/180;
xCoord = Math.cos(radAngle)*circleRadius;
yCoord = Math.sin(radAngle)*circleRadius;
clock.circle.lineTo(xCoord, yCoord);
}
//hour hands
clock.circle.lineStyle(3, 0x000000, 100);
clock.circle.moveTo(100, 0);
circleRadius = 100;
smallerCircleRadius = 90;
for (h=0; h<12;>
{
hourAngle = h*30;
radHourAngle = hourAngle*Math.PI/180;
xCoord1 = Math.cos(radHourAngle)*circleRadius;
yCoord1 = Math.sin(radHourAngle)*circleRadius;
xCoord2 = Math.cos(radHourAngle)*smallerCircleRadius;
yCoord2 = Math.sin(radHourAngle)*smallerCircleRadius;
clock.circle.moveTo(xCoord1, yCoord1);
clock.circle.lineTo(xCoord2, yCoord2);
}
//min. hands
clock.circle.lineStyle(1, 0x000000, 100);
clock.circle.moveTo(100, 0);
circleRadius = 99;
smallerCircleRadius = 91;
for (m=0; m<60;>
{
minuteAngle = m*6;
radMinuteAngle = minuteAngle*Math.PI/180;
xCoord1 = Math.cos(radMinuteAngle)*circleRadius;
yCoord1 = Math.sin(radMinuteAngle)*circleRadius;
xCoord2 = Math.cos(radMinuteAngle)*smallerCircleRadius;
yCoord2 = Math.sin(radMinuteAngle)*smallerCircleRadius;
clock.circle.moveTo(xCoord1, yCoord1);
clock.circle.lineTo(xCoord2, yCoord2);
}
//creates hour hand
clock.createEmptyMovieClip("hoursHand", 20);
clock.hoursHand.lineStyle(6, 0x000000, 100);
clock.hoursHand.moveTo(0, 0);
clock.hoursHand.lineTo(0, -50);
//creates minute hand
clock.createEmptyMovieClip("minutesHand", 30);
clock.minutesHand.lineStyle(5, 0x000000, 100);
clock.minutesHand.moveTo(0, 0);
clock.minutesHand.lineTo(0, -81);
//creates second hand
clock.createEmptyMovieClip("secondsHand", 40);
clock.secondsHand.lineStyle(0, 0xFF0000, 100);
clock.secondsHand.moveTo(0, 0);
clock.secondsHand.lineTo(0, -90);
//creates rotation of hands
clock.onEnterFrame = function ()
{
clockDate = new Date();
seconds = clockDate.getSeconds();
this.secondsHand._rotation = seconds * 6;
minutes = clockDate.getMinutes();
this.minutesHand._rotation = minutes * 6;
hours = clockDate.getHours();
this.hoursHand._rotation = (hours * 30) + (minutes/2);
}
Once I got this down I added a larger rim to the clock and learned how to add a shine to it. See code below. (this is inserted below circle)
// big blue frame
clock.circle.lineStyle(20, 0x94C9FE, 100);
clock.circle.moveTo(114, 0);
bigCircleRadius = 114;
for (a=0; a<360;>
radAngle = a*Math.PI/180;
xCoordBig = Math.cos(radAngle)*bigCircleRadius;
yCoordBig = Math.sin(radAngle)*bigCircleRadius;
clock.circle.lineTo(xCoordBig, yCoordBig);
}
// shining effect on the frame
shineAlpha = 10;
clock.circle.lineStyle(14, 0xFFFFFF, shineAlpha);
clock.circle.moveTo(-114, 0);
shineRadius = 114;
for (a=180; a<=270; a++) {
radAngle = a*Math.PI/180;
if (a>=180 && a<=210) {
shineAlpha = shineAlpha+1;
} else {
shineAlpha = shineAlpha-1;
}
xCoordBig = Math.cos(radAngle)*bigCircleRadius;
yCoordBig = Math.sin(radAngle)*bigCircleRadius;
clock.circle.lineStyle(14, 0xFFFFFF, shineAlpha);
clock.circle.lineTo(xCoordBig, yCoordBig);
}
This adds a lot and I am still impressed with the fact that I did this with scripting. Ya I could have made the clock using the tools in flash but by using the source code of the program to create the clock its that much cooler.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment