ゲームの基本的なゲーム ループの作成に問題があります。私は初期段階にあり、やりたいことは、ボールを一定の速度 (例: 3) で画面の下部に移動することだけです。ボールの位置はメイン パネルの update メソッドで更新され、レンダリング パーツで期待どおりに描画されます。メカニズムに例外はありません。ボールは動きますが、滑らかではありません。ビジュアルは静かで邪魔です。これらのメソッドの実装時間を測定したところ、このカップルの合計実装時間は約 3 ~ 4 ミリ秒でした。このような状況で、適切な FPS は何ですか? 指定された定数値は適切ですか? メカニズムに欠けているものや間違っているものはありますか? よろしくお願いします。メインスレッドのコードブロック。
private final static int MAX_FPS = 200;
private final static int FRAME_PERIOD = 1000 / MAX_FPS;
private final static int MAX_FRAME_SKIPS = 5;
public void run() {
initTimingElements();
long beginTime;
long timeDiff;
int sleepTime;
int framesSkipped;
long beginTime2;
long diff1;
long diff2;
sleepTime = 0;
Canvas canvas;
Log.d(TAG, "Starting game loop");
while (running) {
canvas = null;
try {
canvas = this.surfaceHolder.lockCanvas();
synchronized (surfaceHolder) {
beginTime = System.currentTimeMillis();
framesSkipped = 0;
this.gamePanel.update();
diff1=System.currentTimeMillis()-beginTime;
beginTime2=System.currentTimeMillis();
this.gamePanel.render(canvas);
diff2=System.currentTimeMillis()-beginTime2;
timeDiff = System.currentTimeMillis() - beginTime;
sleepTime = (int) (FRAME_PERIOD - timeDiff);
if (sleepTime > 0) {
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
}
}
while (sleepTime < 0 && framesSkipped < MAX_FRAME_SKIPS) {
this.gamePanel.update();
sleepTime += FRAME_PERIOD;
framesSkipped++;
}
if (framesSkipped > 0) {
Log.d(TAG, "Skipped:" + framesSkipped);
}
framesSkippedPerStatCycle += framesSkipped;
storeStats();
}
} finally {
if (canvas != null) {
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
}