开发者

Traceview shows some methods arent being called for 200ms or more

Traceview shows that updatePhysics() is being called every 10ms or so and it takes about 8ms to run. The methods that I call i开发者_如何学编程nside updatePhysics are only running once every 5 or 6 times updatePhysics() runs, however. Is this simply a bug of Traceview, or what is going on? My game is stuttering a fair amount, so I am trying to figure out what is causing it.

Traceview is generally showing that a lot of my methods go several hundred milliseconds without being called once, even though there appears to be no reason they shouldnt be called. Ideas?

Run Method:

        while (mRun) 
        {
            Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
            Canvas c = null;
            try 
            {
                c = mSurfaceHolder.lockCanvas(null);//null
                {
                    time2 = System.nanoTime()/100000; // Get current time
                    float delta = (time2 - time1)/1000f;

                    if (mMode == STATE_RUNNING) updatePhysics(delta);
                    else updateMenus();
                    doDraw(c);
                    time1 = time2;
                }
            } finally
            {
                // do this in a finally so that if an exception is thrown
                // during the above, we don't leave the Surface in an
                // inconsistent state
                if (c != null) 
                {
                    mSurfaceHolder.unlockCanvasAndPost(c);
                }
            }
        }

Update Physics:

    private void updatePhysics(float delta)
    {
        updateScore(delta);
        updateDifficulty();
    }

EDIT: As you can see here, updateDifficulty is often not called for several hundred ms EVEN THOUGH updatePhysics is being called regularly... It makes no sense. Screenshot of traceview


Most likely somewhere in your thread you're calling thread.Sleep(0) or thread.Yield() or something like that. This will cause a thread to yield to other threads that are being scheduled. And it will often take 10ms or more before the thread gets scheduled back in. I think traceview doesn't understand this fully and counts the time the thread is in suspended state as being active.

Most games use a constant game loop, a real while(true) that never yields to anything.

Some other comments:

I would try the code without the try-catch block, this will slow things down considerabely. Als remove the threadpriority line, this is an OS call and could be slow, and would not add any speed in case of a bug. (It should be fine on normal priority)

Also are you sure this is correct:

time2 = System.nanoTime()/100000; // Get current time
float delta = (time2 - time1)/1000f;

I don't see why you require to devide the delta and the current time. Either convert the time from nanotime to seconds (or whatever you require), and then have a delta in seconds. Or keep time in nanoseconds and convert the delta to seconds. Now you first convert to seconds and then to 1/1000th of a second.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜