How can I handle two SurfaceView in one game loop?

Started by
-1 comments, last by marod 9 years, 3 months ago

Hello everyone

I'm making a game in android, and I have two clases which extends SurfaceView (TitleView and GameView). I have another class too called GameLoopThread that I initialize in the TitleView that draws all my bitmaps in this SurfaceView, but it only works in this view. So when I want to change to the GameView, how I only initialize the GameLoopThread in the TitleView and not in the GameView, this last is a black screen because nothing is drawed on it.

So, I want to know how can I use the same class of the GameLoop to renderize this two views.

For example, I initialice the GameLoop in the Titleview so it renders the buttons in the canvas, NewGame and Exit. But when I press the new game button and start the GameView activity, how this class doesn't have the GameLoop initializated, it doesn't render nothing.

Someone can help me?

This is my GameLoopThread class:


@SuppressLint("WrongCall")
public class GameLoopThread extends Thread {

static final long FPS = 25;
//private GameView view;
private TitleView tview;
private boolean running = false;

public GameLoopThread(TitleView titleView) {
this.tview = titleView;
}
public void setRunning(Boolean run) {
running = run;
}

@Override
public void run() {
long ticksPS = 1000 / FPS;
long startTime;
long sleepTime;
while (running) {
Canvas c = null;
startTime = System.currentTimeMillis();
try {
c = tview.getHolder().lockCanvas();
synchronized (tview.getHolder()) {
tview.onDraw(c);
}
} finally {
if (c != null) {
tview.getHolder().unlockCanvasAndPost(c);
}
}
sleepTime = ticksPS - (System.currentTimeMillis() - startTime);
try {
if (sleepTime > 0) {
sleep(sleepTime);
} else {
sleep(25);
}
} catch (Exception e) {
// TODO: handle exception
}
}
}

} 

Thank you

This topic is closed to new replies.

Advertisement