I am working on a game for Android. I'm following the general program
layout of Lunar Lander, where a SurfaceView has a worker thread that
runs the main game loop. That loop updates the physics and redraws the
screen by creating and drawing to a canvas.

>From the thread class:

    public void run() {
        int i = 0;
        while (i < 10) {
            Canvas canvas = null;
            try {
                canvas = surfaceHolder.lockCanvas(null);
                synchronized (surfaceHolder) {
                    engine.update(); //physics calculations
                    draw(canvas); //draw various bitmaps to the
supplied canvas
                }
            } 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 (canvas != null) {
                        Log.w("dbg", "unlocking and posting canvas " + i);
                    surfaceHolder.unlockCanvasAndPost(canvas);
                }
            }
            i++;

            //delay for a second so I can see what's happening
            try{
                  Thread.currentThread().sleep(1000);//sleep for 1000 ms
                }
                catch(Exception e){
            }

        }
    }




I believe
surfaceHolder.unlockCanvasAndPost(canvas);
is supposed to release the canvas and force the screen to redraw
correct? That's not what I'm seeing. I'm getting strange behavior
where the final draw works, but each previous draw only shows a black
screen.

For a better example, I draw a small character sprite at the left side
of the screen. Then, for each successive frame of 100 frames I
translate the image one to the right. So I expect to see the sprite
slide to the right as the program runs. Instead, the screen is black
the whole time until the end, when the sprite appears already on the
right side of the screen. It's as if the very last call to
unlockCanvasAndPost() is applied to the screen, but the others are
hidden or not drawn.  I've verified that they are, in deed, being
executed.

Any help would be appreciated.
Thank you.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to