Game Development Reference
In-Depth Information
Table 2.3 Thread's Public Methods
DrawThread
The main thread in the project is named DrawThread , which extends Thread and overrides
its run() method to call the GameView's doDraw() method periodically (every 100ms in
the project).
Let's look at the snippet:
/*
* This class extends Thread and overrides its run() method,
* calls the GameView's doDraw() method every 100ms.
*/
public class DrawThread extends Thread{
GameView father ;
SurfaceHolder surfaceHolder ;
boolean flag ;
//thread state, 0-idle, 1-running
boolean isViewOn ;
//GameView display state, 0-hidden, 1-on
public DrawThread(GameView father,SurfaceHolder surfaceHolder){
this . father = father;
this . surfaceHolder = surfaceHolder;
flag = true ;
}
public void run(){
while ( flag ){
while ( isViewOn ){
Canvas canvas = null ;
try {
canvas = surfaceHolder .lockCanvas();
synchronized ( surfaceHolder ){
father .doDraw(canvas);
}
}
catch (Exception e){
e.printStackTrace();
}
finally {
Search WWH ::




Custom Search