我在android中制作游戏,我有两个扩展SurfaceView(TitleView和GameView)的游戏。我有一个GameLoopThread我在TitleView中初始化,它在这个视图中绘制我的所有位图,但它只适用于此视图。因此,当我想要更改为GameView时,我如何仅在TitleView中初始化GameLoopThread而不是在GameView中,这最后是黑屏,因为没有任何内容可以绘制。
有人可以帮助我吗?
这是我的GameLoopThread类:
@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
}
}
}
}
答案 0 :(得分:0)
通过调用sartActivity,
来启动标题视图private static final int TITILE_ACTIVITY_REQUEST_CODE = 1;
Intent titleIntent = new Intent(GameViewActivity.this, TitleViewActivity.class);
startActivityForResult(imageIntent, TITLE_ACTIVITY_REQUEST_CODE);
标题活动完成后,请从您的磁贴活动中调用以下内容,以终止您的标题活动并重新启动游戏视图,
setResult(Activity.RESULT_OK, intent);
finish();
游戏活动视图中的使用此方法接收标题视图已完成的通知
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == TITLE_ACTIVITY_REQUEST_CODE) //* notification from titleview
{
switch (resultCode)
{
case Activity.RESULT_OK:
//* do something,
break;
此代码适用于我的应用程序,如果您有任何其他问题,请与我们联系。