我的游戏出现了一些奇怪的卡顿问题,尽管我的FPS似乎从来没有降到30以下。大约每5秒我的游戏就卡顿一次。由于我的垃圾收集问题,我最初每1-2秒就会卡顿一次,但我已经对这些问题进行了排序,并且经常会在没有垃圾收集的情况下进行15-20秒。
尽管如此,我的游戏仍然周期性地卡顿,即使在logcat中没有列出任何接近卡顿的GC。即使当我拿出我的大部分代码,并简单地将我的“物理”代码设为下面的代码时,我也会遇到这个奇怪的减速问题。我觉得我错过了什么或者忽略了什么。
我放进去的“流逝”代码不应该阻止与FPS变化相关的主要角色的速度变化吗?
任何输入/理论都会很棒。
物理学:
private void updatePhysics()
{
//get current time
long now = System.currentTimeMillis();
//added this to see if I could speed it up, it made no difference
Thread myThread = Thread.currentThread();
myThread.setPriority(Thread.MAX_PRIORITY);
//work out elapsed time since last frame in seconds
double elapsed = (now - mLastTime2) / 1000.0;
mLastTime2 = now;
//measures FPS and displays in logcat once every 30 frames
fps+=1/elapsed;
fpscount+=1;
if (fpscount==30)
{
fps=fps/fpscount;
Log.i("myActivity","FPS: "+fps+" Touch: "+touch);
fpscount=0;
}
//this should make the main character (theoretically) move upwards at a steady pace
mY-=100*elapsed;
//increase amount I translate the draw to = main characters Y
//location if the main character goes upwards
if (mY<=viewY)
{
viewY=mY;
}
}发布于 2010-12-22 20:44:13
尝试删除Log.i()调用,如果还没有这样做的话!
编辑:
或者,删除浮点。elapsed不需要是双精度的,只需保持毫秒的整数计数即可。然后,可以在移动计算中使用mY-=100*elapsed/1000;,并注释掉或移除其他浮点计算(fps、fpscount)。
发布于 2010-12-23 01:29:59
尝试使用traceview分析您的代码。
https://stackoverflow.com/questions/4509147
复制相似问题