Weird stuttering issues that I cannot get rid of (full source included)
I based my game off the lunarlander example and have been having stuttering issues since I first started making it. Nothing I have tried has gotten rid of them so I have gotten to the point where I have spent a few hours creating a stripped down version of the lunarlander example, and put a simple scrolling image in to show the stuttering. NOTE: it is not related to the garbage collector. If you think it is, just look at the log, the garbage collector runs no-where near as often as the stutter appears.
The image that scrolls down the screen stut开发者_Go百科ters approximately every second for about 1/10th of a second on my phone (Motorola Milestone, 2.2). This kind of stuttering does not exactly completely destroy gameplay, but it is very distracting and frustrating. My game also involves a lot of fast scrolling and quick movements so it is generally more obvious there.
If any of you have the time could you take a quick look at this eclipse project and see if:
- It stutters for you on your phone (look closely as it scrolls, it has a small hitch every half second to second and a half)
- If you can see any way to fix the stuttering
I am hoping I simply have a retarded line of code that is causing this whole thing without me realising. I just can't believe that even after stripping out this much it still has the exact same amount of stutter as my full game with 1000 objects, especially since it runs at a solid 60fps on my phone.
EDIT: Have profiled my game on Traceview, it seems fine.
Source download link: http://dl.dropbox.com/u/4972001/LunarLander.rar
I tried it on my phone (nexus one), it was the same for me.
I made a few changes which made it better for me:
Instead of declaring new variables every time during loop, i declared the as properties of the class
Canvas c = null; // becomes
private Canvas c;
// in the loop now just use
c = null
Do this for all the variable you declare over and over again in the doDraw and updatePhysics function.
instead of increasing the viewY until forever, i just put a little if statemant
if(viewY > mCanvasHeight){
viewY -= mCanvasHeight;
}
I am not sure if it's working 100% now but seems much better to me.
It sounds like your scrolling generates lots of garbage that needs to be collected frequently. If you're creating lots of short-term little objects, consider managing your own pool of them. As long as you don't let the pool grow without bounds, this can greatly cut down on the gc preempting your timing.
I tested it on a Verizon DroidX (2.2.1) and it looks like every 3-6 seconds and VERY brief. It seems slightly better with the active wallpaper off and nothing else running. Maybe the graphics subsystem just can't keep up? Have you tried a less graphics intensive image (fewer pixels and fewer colors) just to see what it does?
You are drawing three enormous background bitmaps per-frame... that's more work than you need to do. It may be better to use a BitmapShader
with TileMode set to repeat. Then it's just one call into the native layer.
Add a Paint paint;
member somewhere and change your setSurfaceSize()
to include the following lines:
BitmapShader shader = new BitmapShader(mBackgroundImage,
TileMode.CLAMP, TileMode.REPEAT);
paint = new Paint();
paint.setShader(shader);
Then doDraw()
only needs to be this:
//translate to viewY position
canvas.translate(0,(float)(-viewY));
//draw backgrounds
canvas.drawPaint(paint);
I haven't actually measured this to prove it's faster, but from what I know about graphics APIs it certainly should be.
(Of course if you really want to fly you should be using OpenGL ES...)
精彩评论