私はゲームを開発しています.ユーザーがデバイスの戻るボタンを押した場合、ゲームのプレイ中にスレッドを一時停止し、ユーザーが本当に終了したいかどうかを確認する警告ボックスを表示する必要があります.ユーザーが終了したい場合は、単に終了しますしかし、ユーザーが終了したくない場合に問題が発生します。その後、スレッドを再開する必要があります。しかし、私はそれを行うことはできません..以下は私のコードスニペットです..
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
MyGamePanel.thread.setRunning(false);
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Exit Alert");
alertDialog.setMessage("Do you really want to exit the Game?");
alertDialog.setButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
return;
} });
alertDialog.setButton2("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
MyGamePanel.thread.resume();
MyGamePanel.thread.setRunning(true);
return;
}});
alertDialog.show();
return true;
}
return super.onKeyDown(keyCode, event);
}
以下はスレッド部分です。
@Override
public void run() {
Canvas canvas;
Log.d(TAG, "Starting game loop");
while (running) {
canvas = null;
try {
canvas = this.surfaceHolder.lockCanvas();
synchronized (surfaceHolder) {
// update game state
this.gamePanel.update();
// render state to the screen
// draws the canvas on the panel
this.gamePanel.render(canvas);
}
} finally {
// in case of an exception the surface is not left in
// an inconsistent state
if (canvas != null) {
surfaceHolder.unlockCanvasAndPost(canvas);
}
} // end finally
}
}
update() と render は、MyGamePanel クラスに記述された 2 つの関数です。
私は多くのことを試しましたが、何も機能していません..助けてください...