2012-07-29 53 views
1

一直困惑着我的東西,至今仍然存在,這就是爲什麼AS3中的Timer類沒有足夠快地更新以適應更低的毫秒數?爲什麼AS3中的定時器功能不夠快速地更新0.05秒?

我似乎記得,Flash能夠每20毫秒最多推送一次定時器更新(推薦) - 我爲最近的遊戲創建的定時器每50毫秒更新一次,但即使這樣掙扎。在本地它工作得很好,在網絡環境中,它沒有。

是否有什麼特定的我在這裏做錯了,或者是Flash只是真的不能在網上環境更新足夠快,做一個小數點後兩位的數字?

再次,這在當地正常工作。

我的計時器代碼是在這裏:

package src.gameShared { 

import flash.display.Sprite; 
import flash.utils.Timer; 
import flash.events.TimerEvent; 
import src.main; 


public class timer extends Sprite { 

    private var _updateAmount:int = 50;      // MUST give a whole number if divided by 1000 and less than 100. Recommended above 20. Gives 25, 40 or 50. 
    private var _timer:Timer = new Timer(_updateAmount,0); 
    private var _count:int = new int(); 

    public function startTimer():void{ 
     _timer.addEventListener(TimerEvent.TIMER, updateText, false, 0, true); 
     _timer.start(); 
    } 

    public function stopTimer():void{ 
     _timer.stop(); 
     // Add to score 
    } 

    private function updateText(e:TimerEvent):void{ 
     _count++ 
     dtf_txt.text = returnTime(); 
    } 

    // The below will display double minutes, double seconds and double milliseconds. Most math is based around the double milliseconds. 
    public function returnTime(_score:Boolean = false){ 
     /* 
     ** Minutes takes _count and divides it by (1000/50) = 20 (_updateAmount being 50), to give the value of 1 per second. If _count is 20, this has run 20 times and thus 1000 miliseconds have passed 
     ** So the int can be set to 1. Minutes then checks to see if there is 60 of them (60 seconds). 
     ** Seconds does the same, except it "caps" at 60 before resetting 
     */ 
     var _division:int = (1000/_updateAmount) 

     var minutes:int = Math.floor(_count /_division/60); 
     // Second is 1000 miliseconds 
     var seconds:int = Math.floor(_count /_division) % 60; 
     var milseconds:int = new int(); 

     /* After how many ticks is it a second. So at 50, 20 ticks would be 100 so 1 second, but _count would only show 20. So we *5 to get 100. 
     ** 100 = max num we want, divided by (1000 (1sec) divided by _updateAmount) 
     ** 100/(1000/50) = 5. 5*20 (number of ticks at 50milli per sec) = 100 = what we want 
     */ 
     var _getMil:Number = 100/(1000/_updateAmount) // 5 (20 lots of _updateAmount per second, we want an update on every tick. We do *5 as 20 updates fits into 100 that way) 
     // How many updates to fit into 1000 (1 sec), then divide it by 10 as we want a single digit result 
     var _getSmallMil:Number = (1000/_updateAmount)/10 // 2 (20 lots of _updateAmount per second, we only want an update every 2nd for the whole number, so we half _count) 

     _score == false ? milseconds = Math.floor(_count*_getMil) % 100 : milseconds = Math.floor(_count/_getSmallMil) % 10; 
     // Easier to read code that does the same thing but isn't dynamic: 
     // _score == false ? milseconds = Math.floor(_count*5) % 100 : milseconds = Math.floor(_count/2) % 10; 

     var secString:String = (seconds < 10) ? ("0" + String(seconds)) : String(seconds); 
     var minString:String = (minutes < 10) ? ("0" + String(minutes)) : String(minutes); 

     if(_score){ 
      var milString:String = String(milseconds); 
      if(minutes >= 1){ 
       var _holder:int = int(secString); 
       _holder = _holder + (minutes*60); 
       secString = String(_holder); 
      } 
      return secString+"."+milString; 
     } 
     else{ 
      var milString:String = (milseconds < 10) ? ("0" + String(milseconds)) : String(milseconds); 
      return minString+":"+secString+":"+milString; 
     } 

    } 

} 

}

+0

閃存渲染性能是真窮,除非你使用*所有*正確的技術,即使如此,它也不可能跟上你想要的速度。 – Marty 2012-07-29 23:07:54

+1

執行更新循環所需的最快速度等同於當前幀速率。如果您在幀之間進行多次更新,則會進行不必要的計算。 – zzzzBov 2012-07-30 01:31:34

+0

這個SWF的幀速率是24,每秒更新的數量是20,應該不是說這應該可以嗎? – 2012-07-30 14:49:19

回答

1

嘗試提高幀率。 AS執行仍然與幀相關,所以如果幀速率對於定時器間隔來說太低,則會遇到粒度問題。

0

另外,你沒有在timerEvent更新圖形波谷的updateAfterEvent ...

private function updateText(e:TimerEvent):void{ 
     _count++ 
     dtf_txt.text = returnTime(); 
     e.updateAfterEvent(); 
    } 

// e.updateAfterEvent(); 
相關問題