10

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 stutters 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:

  1. 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)
  2. If you can see any way to fix the stuttering

I am hoping I simply have an errant 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.

I have profiled my game on Traceview; it seems fine.

Source download link: http://dl.dropbox.com/u/4972001/LunarLander.rar

5
  • 2
    did you try profiling your game with traceview? developer.android.com/guide/developing/tools/traceview.html
    – bigstones
    Feb 2, 2011 at 21:11
  • 1
    Yeah I have, it seemed fine - there were even gaps between each "updatePhysics" call, and I couldnt see any unusual gaps without calls.
    – Smills
    Feb 2, 2011 at 21:20
  • 1
    I'm not sure if it's me, but I think there is a barely noticeable stuttering (tried on LG Optimus One 2.2)
    – bigstones
    Feb 2, 2011 at 22:21
  • 1
    Thankyou for testing it bigstones, I am getter that barely noticable stuttering on every phone I test (I just tested it on another phone myself). Does anyone have any idea how to get rid of it?
    – Smills
    Feb 2, 2011 at 23:04
  • I've stumbled on this, don't know if it might be related actually: stackoverflow.com/questions/4985563/…
    – bigstones
    Feb 18, 2011 at 9:31

4 Answers 4

1

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:

  1. 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.

  1. 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.

2
  • Ah, I gave that a go and it does seem to make it a little smoother, but sadly it still stutters on my phone. I managed to try it on a friend's Desire HD and it was liquid smooth on his phone. It only seems to affect some devices which is a bit odd.
    – Smills
    Feb 8, 2011 at 16:09
  • What's the plausibility this change impacts anything? I'm skeptical this code change makes any real difference and I think it's just placebo that you think it's now smoother @Smills: The Desire HD is much more powerful than the Milestone. The Milestone seems a lot slower for games than most newer phones. I've noticed some of my stuttering Milestone apps are liquid smooth on newer phones.
    – rbcc
    Feb 9, 2011 at 6:38
0

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.

2
  • 1
    The part "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." was there at the start :) Good suggestion though, I actually assumed the same problem in the original question, and suggested the same solution. stackoverflow.com/questions/4867732/…
    – Roy T.
    Feb 2, 2011 at 21:29
  • 1
    Yeah, sadly it doesn't seem to be the garbage collector, I wish it was as that would be nice and easy to fix. If you have a look at the example I have eliminated all calls (or at least almost all calls) that generate garbage during runtime.
    – Smills
    Feb 2, 2011 at 21:34
0

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?

0

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...)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.