2012-01-15 77 views
0

我有一個問題,deltaTime我一直在使用。我的代碼如下:DeltaTime問題在程序啓動

public class Time { 
    public static float deltaTime = 0;      
    public static long frameCount = 0;      
    public static float fixedTime = 0;      
    public static float fixedDeltaTime = 0.1f;  
    public static float maxDeltaTime = 0.25f;  

}

我MainThread.java

現在在我的run()函數:

while (running) { 
     canvas = null; 
     try { 
      canvas = this.surfaceHolder.lockCanvas(); 
      synchronized (surfaceHolder) { 
       float newTime = System.nanoTime()/1000000000.0f; 
       Time.deltaTime = frameTime = newTime - currentTime; 

       if(frameTime > Time.maxDeltaTime) 
         frameTime = Time.maxDeltaTime; 

       currentTime = newTime; 

       accumulator += frameTime; 

       while(accumulator > Time.fixedDeltaTime) 
       { 
         this.gamePanel.update(); // where the player and my enemy gets updated       
         accumulator -= Time.fixedDeltaTime; 
       } 

       this.gamePanel.render(canvas); 
       //Perform all non-physics-related updates here 


       ++Time.frameCount; 

       framesSkipped = 0; // resetting the frames skipped 

       timeDiff = System.currentTimeMillis() - beginTime; 
       // calculate sleep time 
       sleepTime = (int)(FRAME_PERIOD - timeDiff); 

        if (sleepTime > 0) { 
          // if sleepTime > 0 we're OK 
          try { 
            Thread.sleep(sleepTime);  
          } catch (InterruptedException e) {} 
        } 

        while (sleepTime < 0 && framesSkipped < MAX_FRAME_SKIPS) { 
          // we need to catch up 
          this.gamePanel.update(); // update without rendering 
          sleepTime += FRAME_PERIOD; // add frame period to check if in next frame 
          framesSkipped++; 
        } 

        if (framesSkipped > 0) { 

          Log.d(TAG, "Skipped:" + framesSkipped); 
        } 
          // for statistics 
          framesSkippedPerStatCycle += framesSkipped; 
          // calling the routine to store the gathered statistics 
          storeStats(); 
       } 
      } finally { 
       // in case of an exception the surface is not left in 
       // an inconsistent state 
        if (canvas != null) { 
        surfaceHolder.unlockCanvasAndPost(canvas); 
        } 
      } //end finally 

    } 

}

在gamePanel.update( )我得到了玩家和我的敵人的更新通知。 我現在的問題是,在我的遊戲中的DeltaTime開始是都知道:高UND因此我的運動是非常快的,因爲我有我的敵人類的更新方法如下:

    x += vX * Time.deltaTime; // velocity * deltaTime 
        y -= vY * Time.deltaTime; 
        //Log.d(TAG, "Time:"+Time.deltaTime+" x: "+x+" y: "+y); 
        vY -= gravity; 

我在幹嘛它是正確的還是我的結構有問題? 感謝您的幫助。

回答

1

您需要在循環開始之前初始化currentTime。你不顯示它是如何被初始化的,但我猜測它從0開始,所以在第一次循環迭代中,deltaTime被設置爲System.nanoTime()返回的當前時間。

P.S.是否有某些原因使您在一個地方使用System.nanoTime(),在另一個地方使用System.currentTimeMillis()?此外,請考慮堅持以毫秒(或納秒)計算所有值,並使用long值並消除浮點計算。

+0

你是對的。 currentTime初始設置爲0.您建議將其設置爲? System.nanoTime()呢?如果運動一開始就是0,那就不會那麼糟糕。感謝迄今 – puelo 2012-01-15 18:36:02

+0

@ P.S.不需要使用毫秒來計算avgFps/currentFPS。當我測試deltaTime時,我不想影響fpsCount。但我想爲我的目的毫秒已經足夠了! – puelo 2012-01-15 18:39:00

+0

@puelo - 我將它初始化爲System.nanoTime()。第一次迭代的運動爲零,因爲遊戲剛剛開始。這隻會發生一幀。 (順便說一下,我的猜測是,你的'beginTime'也有同樣的問題。) – 2012-01-15 18:43:47