1

まず、これは私が取り組んでいる Android ゲームの一部です。ガベージ コレクターは約 3 秒ごとに実行されるため、ゲームに短い (ただし顕著な) 遅延が発生します。コード内の 1 つのメソッドに絞り込みました (以下に貼り付けます)。この部分を使用しない場合、代わりにガベージ コレクターが約 11 秒ごとに実行され、ラグが少なくなります。このコードは、ツリー構造を使用してオブジェクトとの衝突を検出するオブジェクトの一部です。オブジェクト topLeft、topRight、bottomLeft、および bottomRight は、衝突について再帰的にチェックされる同じタイプのオブジェクトです。私は主に、これを各フレームで実行した場合に大量のゴミが蓄積するものがここにあるかどうかを知りたいと思っていました.

public HashSet<Integer> getCollisionTiles(Rect r)
{
    resetTempList();

    if(topLeft!=null && topLeft.containsTiles() && Rect.intersects(r, topLeft.getBounding()))
        topLeft.addCollisionTiles(tempList, r);
    if(topRight != null && topRight.containsTiles() && Rect.intersects(r, topRight.getBounding()))
        topRight.addCollisionTiles(tempList, r);
    if(bottomLeft != null && bottomLeft.containsTiles() && Rect.intersects(r, bottomLeft.getBounding()))
        bottomLeft.addCollisionTiles(tempList, r);
    if(bottomRight != null && bottomRight.containsTiles() && Rect.intersects(r, bottomRight.getBounding()))
        bottomRight.addCollisionTiles(tempList, r);

    return tempList;
}

private void addCollisionTiles(HashSet<Integer> tList, Rect r)
{
    if(level==maxLevel)
    {
        for(Integer i: keyListTiles) 
            tList.add(i);
    }
    else
    {
        if(topLeft!=null && topLeft.containsTiles() && Rect.intersects(r, topLeft.getBounding()))
            topLeft.addCollisionTiles(tList, r);
        if(topRight != null && topRight.containsTiles() && Rect.intersects(r, topRight.getBounding()))
            topRight.addCollisionTiles(tList, r);
        if(bottomLeft != null && bottomLeft.containsTiles() && Rect.intersects(r, bottomLeft.getBounding()))
            bottomLeft.addCollisionTiles(tList, r);
        if(bottomRight != null && bottomRight.containsTiles() && Rect.intersects(r, bottomRight.getBounding()))
            bottomRight.addCollisionTiles(tList, r);
    }
}
4

2 に答える 2

1

を呼び出すとtopLeft.getBounding()、毎回新しい Rectangle が作成されます。

getCollisionTiles()頻繁に呼び出す場合、これは多くのオブジェクトになります。何度も呼び出す前に、外接する四角形を 1 回抽出することができますgetCollisionTiles()

于 2013-01-26T02:08:26.437 に答える
0

わかりました、私はこの問題を解決したと思います。HashMaps は私のプログラムで頻繁に使用され、それらから SparseArray に切り替えました。また、タイルがレンダリングされる方法は、描画されたタイルごとに新しい Rect を作成していたので、この方法をより効果的に使用してガベージを少なくするように最適化しました。

于 2013-01-26T19:26:09.553 に答える