1

So iv been trying to figure out how to draw shapes and images to a surfaceview. iv been using multiple tutorials. but now im lost. i have a Thread that has my game loop and from what i understand im am to have all variable updates in this loop

public void run() {
    Canvas c;
    while (running) {
         c = null;
         try {
             c = surfaceHolder.lockCanvas(null);
             synchronized (surfaceHolder) {
              //Insert methods to modify positions of items in onDraw()
             }
         } finally {
             if (c != null) {
                 surfaceHolder.unlockCanvasAndPost(c);
             }
         }
    }

}

I have a surfaceView that has the onDraw(Canvas canvas) method. Say i want to add oval to my screen how do i do that from my while loop in my thread

i currently have these variables in my thread class

private SurfaceHolder surfaceHolder;
private gameMain gamePanel; //my surfaceview class
private boolean running;

and do i need to have this in my surfaceview class ?

setWillNotDraw(false);

if im doing it completely wrong people point it out. i do not wish to use openGL or any libraries

4

1 回答 1

1

所以我给这个问题的解决方案是改变它说的地方:

try {
    c = view.getHolder().lockCanvas();
    synchronized (view.getHolder()) {
        view.onDraw(c);
    }
    } finally {
        if (c != null) {
           view.getHolder().unlockCanvasAndPost(c);
        }
}

在 GameLoopThread 类的 run() 方法上:

try {
    c = view.getHolder().lockCanvas();
    synchronized (view.getHolder()) {
        if(c != null)
            view.onDraw(c);
    }
    } finally {
        if (c != null) {
           view.getHolder().unlockCanvasAndPost(c);
        }
}

为了完成这个活动并调用另一个活动,我添加了一个名为 GameOver 的方法,该方法运行良好:

private void GameOver() {
    gameLoopThread.setRunning(false);
    Context c = getContext();
    c.startActivity(intent); //intent must be declared
    ((Activity) Pruebas.this.getContext()).finish();
}

我希望这可以帮助别人。

于 2013-08-24T07:33:34.017 回答