Android游戏中的口吃(垃圾收集?)

时间:2012-11-09 01:42:33

标签: java android

为了优化我的游戏,我将其设为网格。物体碱液在网格方块中。因此,为了进行渲染,我收集了摄像机可见区域中的所有对象,并将它们逐层列入:

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);
    }

我该怎样做才能阻止口吃?

由于

1 个答案:

答案 0 :(得分:1)

在您的public ArrayList<Entity> queryRect(OBB2D rect)中,您经常调用neighbourQueryObjs.clear();neighbourQueryObjs.add(e);,分配和释放内存。

要确定这是否真的存在问题,您可以临时注释clear()/add()部分并返回相同的(预先计算好的?)neighbourQueryObjs并查看口吃是否消失。一些事情:

if( neighbourQueryObjs.length() == 0 ) {
    // calculate the neighbourQueryObjs
}
return neighbourQueryObjs;