1

私は私が学んでいる本からこれをつかみました。

public void run()
{
    // this is the method that gets called when the thread is started.

    // first we get the current time before the loop starts
    long startTime = System.currentTimeMillis();

    // start the animation loop
    while (running)
    {
        // we have to make sure that the surface has been created
        // if not we wait until it gets created
        if (!holder.getSurface ().isValid())
            continue;

        // get the time elapsed since the loop was started
        // this is important to achieve frame rate-independent movement,
        // otherwise on faster processors the animation will go too fast
        float timeElapsed = (System.currentTimeMillis () - startTime);

        // is it time to display the next frame?
        if (timeElapsed > FRAME_RATE)
        {
            // compute the next step in the animation
            update();

            // display the new frame
            display();

            // reset the start time
            startTime = System.currentTimeMillis();
        }
    }

    // run is over: thread dies
}

ラグを正しく考慮しており、最適ですか?

つまり、ビデオが1秒間に60回更新できない場合、update()は1秒間に60回呼び出されますか?

ありがとう

4

1 に答える 1

3

ビデオが1秒間に60回更新できない場合、update()は1秒間に60回呼び出されますか?

これに対する答えはノーです。すべてのゲームループの開始時に、最後のゲームループからの経過時間を計算します。これを「timeElapsed」に保存します。

ここから、これを「FRAME_RATE」と比較します。「FRAME_RATE」は誤解を招く名前です。あなたの場合、それはあなたのフレームレートではないからです。フレームレートを60にする必要があります。適切な時間待機するには、FRAME_RATEを1000(ミリ秒)/ 60(フレーム/秒)にする必要があります。

これで、すべての反復の開始時に、経過時間を取得してテストし、次のフレームの時間かどうかを確認します。そうでない場合は、実際の処理またはレンダリングの実行をスキップして、ループを再度実行します。

経過時間が最終的にFRAME_RATE(実際にはフレーム遅延)を超えたら、処理を実行してフレームをレンダリングします。理想的には、これを行うのにかかる時間はフレーム遅延よりも短く、次の反復をスケジュールどおりに開始できるようにします。この場合、update()とdisplay()は1秒間に約60回呼び出されます。

ただし、フレームのレンダリングと処理にかかる時間がフレーム遅延よりも長い場合、次のフレームは次の反復で処理され、遅くなります。この場合、update()とdisplay()は1秒間に60回未満呼び出されます。

于 2012-10-12T02:48:10.293 に答える