2011-02-25 11 views
1

私は基本的なゲームループを以下に示します。しかし、私がキーボードをスライドさせると、ループはより速く実行されます。私は遅延時間が変わらないので、これを理解できません。前 - 後は常に1または2ミリ秒です。なぜこれが起こっているのか誰も説明できますか? -ThanksKeyBoard Slide Out/Configs後のAndroidループの高速化

public abstract class GameLayout extends LinearLayout { 
// App context 
private Context context; 
// Timer period 
private long mPeriod =75; 
// ---------[ GAME LOOP ]----------------- 
protected Handler gameHandler;// = new Handler(); 
private Runnable gameRunnable = new Runnable() { 
    public void run() { 
     long before = SystemClock.uptimeMillis(); 
     gameLoop(); 
     postInvalidate(); // causes UI to be redrawn 
     long after = SystemClock.uptimeMillis(); 
     long delay = mPeriod-(after-before); 
     if (gameHandler != null) { 
      if(delay >0) 
       gameHandler.postDelayed(this, delay); 
      else gameHandler.postDelayed(this, 0); 
     } 
    } 
}; 

protected void startUpdateTimer() { 
    // System.out.println("start timer"); 
    gameHandler = new Handler(); 
    gameHandler.post(gameRunnable); 
} 
enter code here 
protected void stopUpdateTimer() { 
    // System.out.println("stop timer"); 
    gameHandler.removeCallbacks(gameRunnable); 
    gameHandler = null; 
} 

public void setUpdatePeriod(long updateDelay) { 
    mPeriod = updateDelay; 
} 

// ----------------------------------------------------- 
public GameLayout(Context c) { 
    super(c); 
    context = c; 
} 

public GameLayout(Context c, AttributeSet attrs) { 
    super(c, attrs); 
    context = c; 
} 

@Override 
protected void onLayout(boolean changed, int l, int t, int r, int b) { 
    super.onLayout(changed, l, t, r, b); 
    try { 
     // Init game 
     initialize(); 
     /** 
     * start update task. Which will fire onDraw in the future 
     */ 
     startUpdateTimer(); 
    } catch (Exception e) { 
     // bug 
     e.printStackTrace(); 
    } 

答えて

0

onLayoutはかなり頻繁に発生し、確かに回転させることにより一度か二度キックオフされるだろう。 gameHandler/gameRunnableの古いインスタンスを停止していないので、gameRunnableが各レイアウトで1回キックオフされます。実際にあなたのgameLoopソースを見ることなく、知ることは不可能ですが、複数のインスタンスを同時に実行して問題が発生している可能性があります。一度に1つだけgameHandlerを実行していることを確認してくださいハンドラがgameLoopを実行する前にハンドラがそのケースかどうかを調べる)。