0

ゲームを作るためにAndroid開発を始めたばかりです。以前にも触れましたが、基本的なことだけを取り上げました。メインループの設定方法が少し混乱しています。私は XNA (C#) を使用しており、分離された更新/描画ループが気に入っています。

典型的なアンドロイドゲームループがどのように機能するのか疑問に思っていましたか? オンラインで検索したところ、次の 2 つの方法が見つかりました。

public void run() {

         while (running) {

                //Method 1: update is called here 
                view.update();
                Canvas c = null;

                try {

                       c = view.getHolder().lockCanvas();

                       synchronized (view.getHolder()) {

                           //Method 2: update is called inside view.onDraw
                              view.onDraw(c);

                       }

                } finally {

                       if (c != null) {

                              view.getHolder().unlockCanvasAndPost(c);
                       }
                }
         }
   }

2 つのメソッドの例として、ゲーム エンティティの更新を見てみましょう。

        //METHOD1

public void onDraw(Canvas canvas)
{
    for (GameEntity entity : entities)
    {
        entity.update();
        entity.draw(canvas);
    }
}

//END METHOD 1

//METHOD 2

public void update()
{
    for (GameEntity entity : entities)
    {
        entity.update();
    }
}

public void draw(Canvas canvas)
{
    for (GameEntity entity : entities)
    {
        entity.draw(canvas);
    }
}

//END METHOD 2

現在、私はスレッドの経験がまったくないため、XNA が画面の背後で更新/描画ループをどのように行うのかわかりません。しかし、方法 1 を使用すると、すべてのエンティティを 2 回ループ処理する必要があります。1 回目は更新用、もう 1 回は別の描画用です。これによりパフォーマンスが低下するのではないかと心配していますが、オンラインのサンプルでこれを見たことがあります。

私は何かが足りないのでしょうか、それとも正しいのでしょうか?方法 2 はパフォーマンスに関して最も優れていますか?

4

1 に答える 1

0

It matters not how many times you loop since it only matters how many actions you do. And the amount of actions done are basicly the same. since the second "for" only adds an one more supposed "if" for each entety. So its not much.

But it gives you the ability to do only one of the actions and not forced to do both. for example: if I want the game to update 60 times per sec but only draw 40 fps, I can only do that in method 2. This allows you to have a more fluid game with less calculations, but only if you use it right.

If you have the darw and update happen at the same rate, then it is stupid to split them

于 2012-09-03T09:04:21.390 に答える