I am getting some odd stuttering issues with my game even though my FPS never seems to drop below 30. About every 5 seconds my game stutters. I was originally getting stuttering every 1-2 seconds due to my garbage collection issues, but I have sorted those and will often go 15-20 seconds without a garbage collection.
Despite this, my game still stutters periodically even when there is no GC listed in logcat anywhere near the stutter. Even when I take out most of my code and simply make my "physics" code the below code I get this weird slowdown issue. I feel that I am missing something or overlooking something.
Shouldn't that "elapsed" code that I put in stop any variance in the speed of the main character related to changes in FPS?
Any input/theories would be awesome.
Physics:
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&开发者_如何学编程lt;=viewY)
{
viewY=mY;
}
}
Try removing the Log.i()
call, if you haven't already!
Edit:
Alternatively, remove the floating point. elapsed
doesn't need to be double, just keep an integer count of milliseconds instead. You can then use mY-=100*elapsed/1000;
in your movement calculation, and comment out or remove the other floating point calculations (fps
, fpscount
).
Try profiling your code with traceview.
精彩评论