Hello,
I use a thread for drawing to the canvas. By coincidence I recognized
that in the program always jumps into the finally part. After
view.onDraw(c); is reached it steps back to synchronized
(surfaceHolder) and then it steps into the finally part. This happens
every time. This means to me that every time exceptions are thrown.
Can anybody explain why this is so?
public class GameThread extends Thread {
private SurfaceHolder surfaceHolder;
private boolean isRunning = false;
private GameView view;
private long tickFrequency;
private long lastTick = System.currentTimeMillis();
private int framesPerSecond = 25;
public GameThread(SurfaceHolder surfaceHolder, Context context,
Handler handler, GameView view) {
this.surfaceHolder = surfaceHolder;
this.view = view;
tickFrequency = 1000 / framesPerSecond;
}
//Speed should be 25 frames per second
//1 Frame per 40 ms
@Override
public void run() {
Canvas c;
while (isRunning) {
c = null;
//updatePhysics();
if (System.currentTimeMillis() > lastTick + tickFrequency) {
long timeDiff = System.currentTimeMillis() - (lastTick +
tickFrequency);
try {
c = surfaceHolder.lockCanvas(null);
synchronized (surfaceHolder) {
view.onDraw(c);
}
} finally { //do this in a finally so that if an
exception is
thrown during the above we don't leave the
if (c != null) { //surface in an
inconsistent state
surfaceHolder.unlockCanvasAndPost(c);
}
}
lastTick = System.currentTimeMillis() - timeDiff;
}
}
}
public void setRunning(boolean run) {
isRunning = run;
}
}
Regards
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
To unsubscribe, reply using "remove me" as the subject.