/* Gary French, 2007 a simple analog clock based on shapes add this using: #include "Clock.as" */ var isClockInit; if( isClockInit == undefined ) { isClockInit = true; var wrapper = this.createEmptyMovieClip( "ClockWrapper", 1000 ); //time offset of the clock wrapper.offset = 0; if( _root.offset != undefined && parseInt(_root.offset) != NaN ) { wrapper.offset = parseInt(_root.offset); } //defaults to 100 if( _root.alpha != undefined && parseInt(_root.alpha) != NaN ) { wrapper._alpha = parseInt(_root.alpha); } wrapper.radius = Stage.width/2; wrapper.rMarkMinor = 0.015; wrapper.rMarkMajor = 0.050; wrapper.rHandHour = 0.600; wrapper.rHandMinute = 0.700; wrapper.rHandSecond = 0.900; wrapper._x = Stage.width/2; wrapper._y = Stage.height/2; // a {_x:wrapper._x,_y:wrapper._y} anon object could be added if we wanted offset wrapper.createEmptyMovieClip( "ClockFace", 1001 ); wrapper.createEmptyMovieClip( "ClockHour", 1002 ); wrapper.createEmptyMovieClip( "ClockMinute", 1003 ); wrapper.createEmptyMovieClip( "ClockSecond", 1004 ); if( wrapper._alpha != 100 ) { this._alpha = wrapper._alpha; wrapper["ClockFace"]._alpha = wrapper._alpha; wrapper["ClockHour"]._alpha = wrapper._alpha; wrapper["ClockMinute"]._alpha = wrapper._alpha; wrapper["ClockSecond"]._alpha = wrapper._alpha; } wrapper.drawFace = function() { with( wrapper["ClockFace"] ) { clear(); var x,y,dn,r; //width and color of secondary lineStyle(1,0x006000); //angular change dn = 6 * (Math.PI/180); r = wrapper.rMarkMinor*wrapper.radius; for( var n=0; n<2*Math.PI; n+=dn ) { x = (wrapper.radius-r)*Math.cos(n); y = (wrapper.radius-r)*Math.sin(n); moveTo(x,y); x = (wrapper.radius)*Math.cos(n); y = (wrapper.radius)*Math.sin(n); lineTo(x,y); } //width and color of primary lineStyle(3,0x006000); //angular change dn = 30 * (Math.PI/180); r = wrapper.rMarkMajor * wrapper.radius; for( var n=0; n<2*Math.PI; n+=dn ) { x = (wrapper.radius-r)*Math.cos(n); y = (wrapper.radius-r)*Math.sin(n); moveTo(x,y); x = (wrapper.radius)*Math.cos(n); y = (wrapper.radius)*Math.sin(n); lineTo(x,y); } } } wrapper.drawFace(); wrapper.onEnterFrame = function () { var x,y,n; var currentDate = new Date(); //(current seconds) * (full rot)/(seconds in rot); //convert to rads; //rotate - 90 degrees n = currentDate.getSeconds() * (360/60) * (Math.PI/180) - (Math.PI/2); with( wrapper["ClockSecond"] ) { clear(); x = this.radius*this.rHandSecond*Math.cos(n); y = this.radius*this.rHandSecond*Math.sin(n); /*simple hands lineStyle(1,0xA00000); moveTo(0,0); lineTo(x,y); */ /*fancy hands */ var dn = 3 * (Math.PI/180); var x0 = this.radius*this.rHandSecond*Math.cos(n-dn)*0.7; var y0 = this.radius*this.rHandSecond*Math.sin(n-dn)*0.7; var x1 = this.radius*this.rHandSecond*Math.cos(n+dn)*0.7; var y1 = this.radius*this.rHandSecond*Math.sin(n+dn)*0.7; lineStyle(1,0xA00000); moveTo(0,0); lineTo(x,y); lineStyle(2,0xA00000); moveTo(x0,y0); lineTo(x1,y1); } //(current minutes) * (full rot)/(minutes in rot); //convert to rads; //rotate - 90 degrees n = currentDate.getMinutes() * (360/60) * (Math.PI/180) - (Math.PI/2); with( wrapper["ClockMinute"] ) { clear(); x = this.radius*this.rHandMinute*Math.cos(n); y = this.radius*this.rHandMinute*Math.sin(n); /*simple hands lineStyle(5,0x000000); moveTo(0,0); lineTo(x,y); */ /*fancy hands */ var dn = 6 * (Math.PI/180); var x0 = this.radius*this.rHandMinute*Math.cos(n-dn)*0.7; var y0 = this.radius*this.rHandMinute*Math.sin(n-dn)*0.7; var x1 = this.radius*this.rHandMinute*Math.cos(n+dn)*0.7; var y1 = this.radius*this.rHandMinute*Math.sin(n+dn)*0.7; lineStyle(0,0x000000); beginFill(0x000000); moveTo(0,0); lineTo(x0,y0); lineTo(x,y); lineTo(x1,y1); lineTo(0,0); endFill(); } //works for either 12 or 24 format, but is a wee bit trickier to handle the fractional hour //the equation is broken up so it can be understood a bit easier //(current hours) * (minutes in hour) + (current minutes) // * (full rot)/(hours in rot expressed in minutes); //convert to rads; //rotate - 90 degrees n = (currentDate.getHours()+wrapper.offset) * 60 + currentDate.getMinutes(); n *= (360/(12*60)) n *= (Math.PI/180); n -= (Math.PI/2); with( wrapper["ClockHour"] ) { clear(); x = this.radius*this.rHandHour*Math.cos(n); y = this.radius*this.rHandHour*Math.sin(n); /*simple hands lineStyle(5,0x000000); moveTo(0,0); lineTo(x,y); */ /*fancy hands */ var dn = 8 * (Math.PI/180); var x0 = this.radius*this.rHandHour*Math.cos(n-dn)*0.7; var y0 = this.radius*this.rHandHour*Math.sin(n-dn)*0.7; var x1 = this.radius*this.rHandHour*Math.cos(n+dn)*0.7; var y1 = this.radius*this.rHandHour*Math.sin(n+dn)*0.7; lineStyle(0,0x000000); beginFill(0x000000); moveTo(0,0); lineTo(x0,y0); lineTo(x,y); lineTo(x1,y1); lineTo(0,0); endFill(); } } }