2

ゲームを最適化するために、グリッドベースにしました。オブジェクトはグリッドの正方形に横たわっています。したがって、レンダリングを行うために、カメラに表示されている領域のすべてのオブジェクトを収集し、レイヤーごとにリストに入れます。

public ArrayList<Entity> queryRect(OBB2D rect)
{
    neighbourQueryObjs.clear();
    ArrayList<Region> reg = determineNeighbourRegions(rect);
    for(int i = 0; i < Entity.MAX_LAYERS; ++i)
    {
        for(Region r : reg)
        {
            for(Entity e : r.getStatic(i))
            {
                if(!neighbourQueryObjs.contains(e) && e.getRect().overlaps(rect))
                {
                    neighbourQueryObjs.add(e);
                }
            }

            for(Entity e : r.getDynamic(i))
            {
                if(!neighbourQueryObjs.contains(e) && e.getRect().overlaps(rect))
                {
                    neighbourQueryObjs.add(e);
                }
            }
        }
    }

    return neighbourQueryObjs;

}

これはうまくいきます。ゲームはとても速いです。私はこれをeveyフレームと呼んでいます。問題は、1秒または2秒ごとに、ゲームが約2〜300ミリ秒間フリーズすることです。

ガベージコレクションだと思います。それでいいの?

GCを使用する必要がないように、このようなものを事前に割り当てる方法はありますか?私はメモリを無駄にすることを気にしません、メモリは問題ありません。

これが私のゲームループです。

public void run() {
        Canvas canvas;

        long beginTime;     // the time when the cycle begun
        long timeDiff;      // the time it took for the cycle to execute
        int sleepTime;      // ms to sleep (<0 if we're behind)
        int framesSkipped;  // number of frames being skipped 

        sleepTime = 0;

        while (running) 
        {
            canvas = null;
            // try locking the canvas for exclusive pixel editing
            // in the surface
            try 
            {

                // we have to make sure that the surface has been created
                // if not we wait until it gets created
                if (!holder.getSurface ().isValid())
                    continue;
                canvas = this.holder.lockCanvas();
            synchronized (holder) 
            {
                    beginTime = System.currentTimeMillis();
                    framesSkipped = 0;  // resetting the frames skipped
                    // update game state
                    update();
                    // render state to the screen
                    // draws the canvas on the panel
                    display(canvas);
                    // calculate how long did the cycle take
                    timeDiff = System.currentTimeMillis() - beginTime;
                    // calculate sleep time
                    sleepTime = (int)(FRAME_PERIOD - timeDiff);

                    if (sleepTime > 0) 
                    {
                        // if sleepTime > 0 we're OK
                        try 
                        {
                            // send the thread to sleep for a short period
                            // very useful for battery saving
                            Thread.sleep(sleepTime);
                        } catch (InterruptedException e) {}
                    }

                    while (sleepTime < 0 && framesSkipped < MAX_FRAME_SKIPS) 
                    {
                        // we need to catch up
                        // update without rendering
                        update();
                        // add frame period to check if in next frame
                        sleepTime += FRAME_PERIOD;
                        framesSkipped++;
                    }
                }
            } 
            finally 
            {
                // in case of an exception the surface is not left in
                // an inconsistent state
                if (canvas != null) 
                {
                    holder.unlockCanvasAndPost(canvas);
                }
            }   // end finally
        }
    }
    protected void onDrawTransformed(GraphicsContext g)
    {   
        OBB2D view = g.getCamera().getCamRect(getWidth(), getHeight());

        ArrayList<Entity> ents = world.queryRect(view);
        for(Entity e : ents)
        {
            e.draw(g);
        }
        //city.draw(g);
        //vehicle.draw(g);
    }

吃音を止めるにはどうすればよいですか?

ありがとう

4

1 に答える 1

1

あなたpublic ArrayList<Entity> queryRect(OBB2D rect)は常にneighbourQueryObjs.clear();andを呼び出していneighbourQueryObjs.add(e);ます。これは、メモリの割り当てと割り当て解除を行います。

これが本当に問題であるかどうかを確認するには、パーツを一時的にコメントアウトしてclear()/add()、常に同じ(事前計算済み?)を返しneighbourQueryObjs、スタッターがなくなったかどうかを確認します。線に沿った何か:

if( neighbourQueryObjs.length() == 0 ) {
    // calculate the neighbourQueryObjs
}
return neighbourQueryObjs;
于 2012-11-09T02:16:14.907 に答える