Create three movieclips for clock hand: hour, minute, and second.
The important thing is that you need to move the center registration point of each hand to the axis of the hand (probably one end), so that the movie clip will rotate around that point.
Attach these movieclips to stage.
Press Ctrl+L, then drop and draw these movieclips from library to stage, and assign instant name for each.
Copy the code into your document class.Don't know what is document class? See actionscript document class. In line 32, I add a listener to stage, so the three clock hands will be rotated in every second.
package
{
import flash.display.*;
import flash.events.Event;
public class Clock extends Sprite
{
private var currentHour:int = 0;
private var currentMin:int = 0;
private var currentSec:int = 0;
public function Clock() {
init();
}
public function init():void{
background.x = stage.stageWidth/2;
background.y = stage.stageHeight/2;
clockHour.x = stage.stageWidth/2;
clockHour.y = stage.stageHeight/2;
clockMin.x = stage.stageWidth/2;
clockMin.y = stage.stageHeight/2;
clockSec.x = stage.stageWidth/2;
clockSec.y = stage.stageHeight/2;
clockHour.height = 120;
clockMin.height = 160;
clockSec.height = 170;
// Adding event listener to stage
stage.addEventListener(Event.ENTER_FRAME, rotatePointer);
}
public function rotatePointer(e:Event):void
{
//getting time
var time = new Date();
currentHour= time.getHours();
currentMin = time.getMinutes();
currentSec = time.getSeconds();
if (currentHour>12) {
currentHour = currentHour-12;
}
if (currentHour<1) {
currentHour = 12;
}
//set property of _rotation of hour,minute,second hand.
currentHour = currentHour*30+ (currentMin/60) *30;
currentMin = currentMin*6+ (currentSec/60) *6;
currentSec = currentSec*6;
// e.target is the reference to the MovieClip calling the event
clockHour.rotation = currentHour;
clockMin.rotation = currentMin;
clockSec.rotation = currentSec;
}
}
}
|
Tutorial
|