Here's an alternative approach. The idea is to to store the current start time once. Then every time you want to update the clock (onTick), you check how much time has passed since the start time and create a new date object that represents the start time + the time passed scaled with a factor. If the factor is larger than 1 then the clock moves faster than realtime, if it's lower it moves slower.
If you run onTick often you get a smooth animation, but you can run it once a second or whichever interval you want.
// store current start time in milliseconds
var startTime : Number = new Date().getTime();
// the delay in the Timer doesn't affect the time
// displayed by the clocks, but only determines
// how often the clocks should be updated (redrawn)
var ct:Timer = new Timer(50);
ct.addEventListener(TimerEvent.TIMER, onTick);
ct.start();
function onTick(event:TimerEvent):void {
// first clock should run at normal speed so we send in 1 as scale factor
var timeDataOne : Object = calculateTime(1);
// second clock at double speed (send in 0.5 to run at half speed)
var timeDataTwo : Object = calculateTime(2);
one_second_hand_mc.rotation = 180 + (timeDataOne.s * 6);
one_minute_hand_mc.rotation = 180 + (timeDataOne.m * 6);
one_hour_hand_mc.rotation = 180 + (timeDataOne.h/12) * 360;
two_second_hand_mc.rotation = 180 + (timeDataTwo.s * 6);
two_minute_hand_mc.rotation = 180 + (timeDataTwo.m * 6);
two_hour_hand_mc.rotation = 180 + (timeDataTwo.h/12) * 360;
}
function calculateTime(clockSpeed : Number = 1):Object {
// current time in milliseconds
var nowTime : Number = new Date().getTime();
// how many milliseconds have passed from the start time
var timePassed : Number = nowTime - startTime;
// create a new date object which should hold the time to display
var displayTime : Date = new Date();
// here we set the date object, which is based on the start time
// plus the time passed multiplied with a scale factor clockSpeed.
// When clockSpeed is one, displayTime will match the current time.
displayTime.setTime(startTime + timePassed * clockSpeed);
// calculate seconds, minutes and hours (and since the clock is analog
// we use Number so we don't get discreet steps for the clock hands)
var s : Number = displayTime.getSeconds() + (displayTime.getMilliseconds()/1000);
var m : Number = displayTime.getMinutes() + s / 60;
var h : Number = (displayTime.getHours() % 12) + m / 60;
return { s: s, m: m, h: h };
}
2. uint is an integer number. But a step is a float (0.1 and 1 / 600). I can use uint for 'secondStep'. But for uniformity I use 'Number'. Maybe it is better to use uint for 'secondStep'. – Emin A. Alekperov Oct 05 '12 at 22:51