2011-06-09 50 views
0

我正在開發一款手機遊戲,當遊戲失去焦點時需要暫停遊戲時間,例如當來電時。除了當遊戲重新獲得焦點時,時間調整爲彷彿它一直在運行。如果有人在他們回來的時候打了3分鐘的電話,他們應該在那裏找到比賽時間。AS3需要暫停的時間

這裏是我的代碼:

public function showTime(event:Event){ 
      gameTime = getTimer()-gameStartTime; 
      timeDisplay.text = "Time: "+clockTime(gameTime); 
     } 

    public function clockTime(ms:int) { 
     var seconds:int = Math.floor(ms/1000); 
     var minutes:int = Math.floor(seconds/60); 
     seconds -= minutes*60; 

     var timeString:String = minutes+":"+String(seconds+100).substr(1,2); 
     return timeString; 
    } 


    public function onActivate(event:Event):void { 
     addEventListener(Event.ENTER_FRAME, showTime); 
    } 


    public function onDeactivate(event:Event):void { 
     removeEventListener(Event.ENTER_FRAME, showTime); 
    } 

我一直在谷歌搜索這兩天,我堅持。有人能指點我正確的方向嗎?由於我對AS3相當陌生,所以一些示例代碼也是有益的。謝謝!

豐富

回答

0

你計算基於當前時間的時間 - gameStartTime。您需要考慮延遲時間(通過在onActivate()onDeactivate()函數中使用getTimer()來計算停止的總時間,然後將該差值添加到gameStartTime),或者如果您不想更改gameStartTime,則計算基於時間的時間在德爾塔時間。喜歡的東西:

private var m_prevTime:int = 0; 

public function startGame():void 
{ 
    // set the prevTime only when you start the game, otherwise you'll 
    // take into account all the time for flash to get going, your init, 
    // opening screens etc 
    this.m_prevTime = getTimer(); 
} 

public function showTime(event:Event) 
{ 
    // get the difference in time 
    var currTime:int = getTimer(); 
    gameTime += currTime - this.m_prevTime // currTime - prevTime = delta time 
    this.m_prevTime = currTime; // hold the current time 

    // display it 
    timeDisplay.text = "Time: "+clockTime(gameTime); 
} 


public function onActivate(event:Event):void 
{ 
    // we're reactivating, so make sure prev time is updated 
    this.m_prevTime = getTimer(); 
    addEventListener(Event.ENTER_FRAME, showTime); 
} 

從更新回來時,你會在一個幀的時間錯過了,但它不是noticable(或者你可以減去一幀的在onActivate()功能時)

+0

謝謝。一旦你描述延遲考慮,我決定走這條路線,因爲它對我來說很有意義。我添加了兩個新的getTimer(),並從一開始就減去了停頓,然後從gameStartTime中取出了這個延遲量。還沒有在設備上使用它,但它可以在Flash中使用。非常感謝你的幫助! – Rich 2011-06-09 16:10:42

0

而不是使用getTimer()的,你應該創建一個定時器對象並添加事件監聽器「嘀」更新時鐘。你甚至可以將它設置爲每秒一次,所以你不必轉換MS。

public function onActivate(event:Event):void { 
    myTimer.start(); 
} 


public function onDeactivate(event:Event):void { 
    myTimer.stop(); 
} 

看到計時器參考:http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/flash/utils/Timer.html

+0

不,getTimer()是一個全球性的函數,返回毫秒的數量,因爲該方案開始 – divillysausages 2011-06-09 15:18:23

+0

呵呵,有意思。不知道,我沒有做太多的遊戲。但是,我的答案依然如此。他應該實例化一個計時器對象,以便他可以啓動/停止它並向事件添加事件。 – 2011-06-09 15:20:43

1

您可以檢查這項秒錶教程中,我寫了一段時間後:

http://www.popamihai.com/2010/10/flex/using-the-timer-class-in-flex-to-display-a-simple-stopwatch/

這是寫使用Flex Builder 3,但你可以很容易地複製/粘貼代碼。以查看源代碼示例啓用也包含

<?xml version="1.0" encoding="utf-8"?> 
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" verticalAlign="middle" 
backgroundColor="white" viewSourceURL="srcview/index.html" creationComplete="init()"> 

<mx:Script> 
    <![CDATA[ 
     import flash.utils.getTimer; 
     import flash.utils.Timer; 
     import flash.events.TimerEvent; 

     private const TIMER_INTERVAL:int = 50; 
     private var timerAtStart:Number = 0; 
     private var timerAtPause:Number = 0; 
     private var t:Timer; 
     private var d:Date; 

     [Bindable] private var sec:int = 0; 
     [Bindable] private var min:int = 0; 
     [Bindable] private var mls:int = 0; 

     private function init():void { 
      t = new Timer(TIMER_INTERVAL); 
      t.addEventListener(TimerEvent.TIMER, updateTimer); 
     } 

     private function updateTimer(evt:TimerEvent):void{ 
      d = new Date(getTimer() - timerAtStart + timerAtPause); 
      min = d.minutes; 
      sec = d.seconds; 
      mls = d.milliseconds; 
     } 

     private function startPauseTimer(event:MouseEvent):void{ 
      if (event.target.label == "start") { 
       event.target.label = "pause"; 
       timerAtStart = getTimer(); 
       t.start(); 
      } else { 
       event.target.label = "start"; 
       timerAtPause = min * 60000 + sec * 1000 + mls; 
       t.stop(); 
      } 

     } 

    ]]> 
</mx:Script> 

<mx:Label text="{min + ' : ' + sec + ' : ' + mls}" fontSize="16" fontWeight="bold"/> 
<mx:HBox> 
    <mx:Button label="start" click="startPauseTimer(event)"/> 
</mx:HBox> 
</mx:Application> 
+0

小時呢?只需添加小時* 3600000? – Roger 2016-09-24 16:55:46