2012-10-04 54 views
-1

一個人如何啓動兩個動畫模擬時鐘顯示相同的初始時間,然後做一個快兩倍,另一方面使那一秒變成一個半秒等等,以便它比賽過「實時」時鐘?訣竅是他們需要開始顯示當前時間。如何使動畫模擬時鐘運行快兩倍,另一個在相同的初始時間開始,而

我知道這可能是簡單的,但我每次乘數增加時鐘兩個變量的時候它開始在不正確的時間,並跳過蜱。

這是我作爲一個起點的ActionScript(兩個時鐘是相同的):

var now:Date; 
var ct:Timer = new Timer(1); 
ct.addEventListener(TimerEvent.TIMER, onTick); 
ct.start(); 

function onTick(event:TimerEvent):void { 
now = new Date(); 
var s:uint = now.getSeconds(); 
var m:uint = now.getMinutes(); 
var h:uint = now.getHours(); 
/*CLOCK ONE*/ 
one_second_hand_mc.rotation = 180 + (s * 6); 
one_minute_hand_mc.rotation = 180 + (m * 6); 
one_hour_hand_mc.rotation = 180 + (h * 30) + (m/2); 
/*CLOCK TWO*/ 
two_second_hand_mc.rotation = 180 + (s * 6); 
two_minute_hand_mc.rotation = 180 + (m * 6); 
two_hour_hand_mc.rotation = 180 + (h * 30) + (m/2); 
} 

回答

1

這是另一種方法。這個想法是一次存儲當前的開始時間。然後,每次要更新時鐘(onTick)時,都會檢查自開始時間以來經過的時間,並創建一個新的日期對象,該對象表示開始時間+ 的時間長度,其因子爲。如果因子大於1,那麼時鐘移動的速度比實時更快,如果移動速度越慢,移動越慢。

如果您經常在onTick上運行,您會獲得平滑的動畫,但您可以每秒運行一次或以任意間隔運行一次。

// 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 }; 
} 
+0

這工作。我也喜歡時間是如何計算在自己的對象。 – Dylan

1
  1. 這是一個糟糕的practiсe設置一個計時器延遲小於20毫秒少: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/utils/Timer.html#delay

  2. 嘗試類似的東西(我爲了簡單起見只顯示了幾秒):

var now:Date;
var ct:Timer = new Timer(500);
ct.addEventListener(TimerEvent.TIMER,onTick);

now = new Date();
var s:uint = 180 + 6 * now.getSeconds();
/CLOCK ONE/
one_second_hand_mc.rotation = S;
/CLOCK TWO/
two_second_hand_mc.rotation = S;

ct.start();

var secondStep:Number = 6; // 360/60(錶盤上的分區)
var minutStep:Number = 0.1; // 6/60(秒分鐘)
var hourStep:Number = 1/600; //三千六分之六(以小時秒)

功能onTick(事件:TimerEvent):(!(ct.currentCount%2))空隙{

/CLOCK ONE/
如果{
one_second_hand_mc.rotation + = secondStep;
one_minute_hand_mc.rotation + = minuteStep;
one_hour_hand_mc.rotation + = hourStep;
}

/CLOCK TWO/
two_second_hand_mc.rotation + = secondStep;
two_minute_hand_mc.rotation + = minuteStep;
two_hour_hand_mc.rotation + = hourStep;
}

+0

我已經設法讓它在秒鐘內工作,但在分鐘和小時仍然有問題。變量方程對於幾分鐘和幾小時會是什麼?我嘗試了很明顯的做法,將分鐘數乘以6,將時間乘以30加上分鐘數除以2,但這似乎是錯誤的。 – Dylan

+0

這工作幾秒鐘。 – Dylan

+0

我已通過添加分鐘和小時的代碼更新了我的answare。 –

相關問題