gpt4 book ai didi

android - 自动退出 SurfaceView

转载 作者:太空宇宙 更新时间:2023-11-03 13:04:34 24 4
gpt4 key购买 nike

我正在为 Android 2.x 开发一款游戏。当玩家松动时,我想自动退出并返回到游戏的上一个菜单。这可能是一个简单的问题,但我该怎么做呢?

我有一个 MenuActivity,它调用 GameActivity,它调用游戏逻辑所在的 GameView(SurfaceView 的实现)。

更新:不,等等,它比这更复杂。为了制作这款游戏​​,我遵循了使用线程的 MoonLander 示例。

游戏 View :

public class GameActivity extends Activity
{
GameView gameView;
GameThread gThread;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
gameView = new GameView(this);
setContentView(gameView);

gThread = gameView.getThread();
gThread.doStart();
}

游戏 View :

package com.cp.balldrop;

public class GameView extends SurfaceView implements SurfaceHolder.Callback
{
class GameThread extends Thread
{


public GameThread(SurfaceHolder sHolder, Context context, Handler handler)
{
//Code
}

public void doStart()
{
synchronized (mSurfaceHolder)
{

}
}

@Override
public void run()
{
while (running)
{
Canvas c = null;
try
{
c = mSurfaceHolder.lockCanvas(null);
synchronized (mSurfaceHolder)
{
doDraw(c);
}
}
finally
{
if (c != null)
{
mSurfaceHolder.unlockCanvasAndPost(c);
}
}
}
}

protected void doDraw(Canvas canvas)
{
//code
}
}

/** Handle to the application context, used to e.g. fetch Drawables. */
private Context mContext;

/** Pointer to the text view to display "Paused.." etc. */
private TextView mStatusText;

/** The thread that actually draws the animation */
private GameThread gThread;

public GameView(Context context)
{
super(context);

// register our interest in hearing about changes to our surface
SurfaceHolder holder = getHolder();
holder.addCallback(this);

// create thread only; it's started in surfaceCreated()
gThread = new GameThread(holder, context, new Handler()
{
@Override
public void handleMessage(Message m)
{
// mStatusText.setVisibility(m.getData().getInt("viz"));
// mStatusText.setText(m.getData().getString("text"));
}
});

setFocusable(true);
}

public GameThread getThread()
{
return gThread;
}

public void surfaceCreated(SurfaceHolder holder)
{
gThread.start();
}

@Override
public void surfaceDestroyed(SurfaceHolder holder)
{
boolean retry = true;
gThread.setRunning(false);

while (retry)
{
try
{
gThread.join();
retry = false;
}
catch (InterruptedException e)
{
}
}
}
}

最佳答案

在我的线程中,我从 surfaceview 的 doTouch 中调用了以下方法:

public void doLose() {
synchronized (mSurfaceHolder) {
//quit to mainmenu
((Activity) mContext).finish();
}
}

这似乎关闭了表面 View 和托管它的 Activity ,让我回到主菜单 Activity 中。注意转换和预定义的上下文。

关于android - 自动退出 SurfaceView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6638465/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com