Simple Flash Analog Clock - AS3









Download clock.zip, 11 KB (Flash CS3|CS4 professional)



ActionScript Code:

// Developed by Anil kumar N D
// anilkumarnd@gmail.com
// ActionScript 3.0
// 28-August-2009
// http://flash-workshop.blogspot.com/

package {

import flash.display.MovieClip;

import flash.events.Event;

import flash.text.TextField;

import flash.text.TextFieldAutoSize;

import flash.text.TextFormat;

public class clock extends MovieClip {

var mc_hourHand:MovieClip;

var mc_minHand:MovieClip;

var mc_secHand:MovieClip;

var dte_currentDate:Date = new Date();

var num_hour:Number;

public function clock():void {

numbers();

this.graphics.lineStyle(15,0xFF9900);

this.graphics.drawCircle(stage.stageWidth/2,stage.stageHeight/2,stage.stageWidth/2-10);

mc_hourHand=drawLine(100,12);

addChild(mc_hourHand);

mc_hourHand.x=stage.stageWidth/2;

mc_hourHand.y=stage.stageHeight/2;

mc_minHand=drawLine(140,8);

addChild(mc_minHand);

mc_minHand.x=stage.stageWidth/2;

mc_minHand.y=stage.stageHeight/2;

mc_secHand=drawLine(170,5);

addChild(mc_secHand);

mc_secHand.x=stage.stageWidth/2;

mc_secHand.y=stage.stageHeight/2;

stage.addEventListener(Event.ENTER_FRAME,enterFrameHandler);


this.graphics.beginFill(0xFF9900,1);

this.graphics.drawCircle(stage.stageWidth/2,stage.stageHeight/2,15);

this.graphics.endFill();



}


private function drawLine(Length:Number,thickness:Number):MovieClip {

var mc_line:MovieClip = new MovieClip();

mc_line.graphics.lineStyle(thickness,0xFF9900);

mc_line.graphics.lineTo(0,Length-Length-Length);

return mc_line;

}

private function enterFrameHandler(IN_Event:Event):void {

dte_currentDate = new Date();

num_hour=dte_currentDate.hours;

if (num_hour>12) {

num_hour-=12;
}

mc_hourHand.rotation=(num_hour+(dte_currentDate.minutes/60)+(dte_currentDate.seconds/3600))*30;

mc_minHand.rotation=(dte_currentDate.minutes+(dte_currentDate.seconds/60))*6;

mc_secHand.rotation=dte_currentDate.seconds*6;

}

private function numbers():void{


var format:TextFormat = new TextFormat();
format.font = "Verdana";
format.color = 0xFF9900;
format.size = 35;
format.bold = true;



var txt_TextField_12:TextField = new TextField();

txt_TextField_12.selectable = false;

txt_TextField_12.defaultTextFormat = format;

txt_TextField_12.autoSize = TextFieldAutoSize.CENTER;

txt_TextField_12.text = "12";

addChild(txt_TextField_12);

txt_TextField_12.x = stage.stageWidth/2 -txt_TextField_12.width/2 ;

txt_TextField_12.y = 25;


var txt_TextField_3:TextField = new TextField();

txt_TextField_3.selectable = false;

txt_TextField_3.defaultTextFormat = format;

txt_TextField_3.autoSize = TextFieldAutoSize.CENTER;

txt_TextField_3.text = "3";

addChild(txt_TextField_3);

txt_TextField_3.x = stage.stageWidth - 50;

txt_TextField_3.y = stage.stageHeight/2 -txt_TextField_3.height/2;



var txt_TextField_6:TextField = new TextField();

txt_TextField_6.selectable = false;

txt_TextField_6.defaultTextFormat = format;

txt_TextField_6.autoSize = TextFieldAutoSize.CENTER;

txt_TextField_6.text = "6";

addChild(txt_TextField_6);

txt_TextField_6.x =stage.stageWidth/2 -txt_TextField_6.width/2 ;

txt_TextField_6.y =stage.stageHeight - 70 ;



var txt_TextField_9:TextField = new TextField();

txt_TextField_9.selectable = false;

txt_TextField_9.defaultTextFormat = format;

txt_TextField_9.autoSize = TextFieldAutoSize.CENTER;

txt_TextField_9.text = "9";

addChild(txt_TextField_9);

txt_TextField_9.x =30;

txt_TextField_9.y =stage.stageHeight/2 -txt_TextField_9.height/2 ;

}


}
}




Comments

hady said…
Thanks very much dear anil
for this great blog and for your attension on yahoo answers on my question

thanks again
hady
Unknown said…
thanks for the nice script for a clock...

Work from home India
Unknown said…
Thanks for our script
can you please explain me how to position the hour hand ,minute hand and second hand according to the current time
Anonymous said…
That's excellent post. I must say you have made it really easy to understand by using such easy language. I am going to share this to all my colleagues so they can take advantage of this tutorial too.
flash website developer
onlineseowala said…
excellent post.Thanks for share

Lawsuit Loans

Popular posts from this blog

AS3 - Access objects from external SWF files

Flash CS3 - XML Guestbook (AS3)