在发现开始一个新的意图可能不是通知 GameOver 用户的正确方式之后,我现在正在与 runOnUiThread、Runnable 和对话框作斗争。
问题:我将如何以及在哪里实现 Dialog.show() 来通知用户游戏已经结束?我正在实施一个铁路竞赛游戏。在这种情况下,当火车碰撞发生游戏结束时,我想在这里显示对话框。
我已经尝试过 runOnUiThread、Runnable 和 Async 线程。想不通。我想在 MainGamePanel 的碰撞发生方法中显示对话框。
我有一个游戏。活动 AndroidGameActivity :
private static final String TAG = AndroidGame.class.getSimpleName();
public static Vibrator v;
private GameDatabaseOperations gameDatabaseOperations;
private int currentDate;
private int todayDate;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
try{
currentDate = Calendar.getInstance().get(Calendar.DATE);
gameDatabaseOperations = new GameDatabaseOperations(this);
gameDatabaseOperations.createWriteModeDatabase();
Cursor crDate = gameDatabaseOperations.fetchTodayDate();
startManagingCursor(crDate);
if(crDate!= null && crDate.getCount() >0){
crDate.moveToFirst();
todayDate = crDate.getInt(0);
}
if(currentDate != todayDate ){
gameDatabaseOperations.updateTodayDefaultScore(0);
gameDatabaseOperations.updateTodayDate(currentDate);
Cursor cr1Date = gameDatabaseOperations.fetchTodayDate();
startManagingCursor(cr1Date);
if(cr1Date!= null && cr1Date.getCount() >0){
cr1Date.moveToFirst();
todayDate = cr1Date.getInt(0);
}
}
gameDatabaseOperations.closeDatabase();
}
catch(Exception e){
e.printStackTrace();
}
FrameLayout fl = new FrameLayout(this);
setContentView(fl);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout pause = (LinearLayout) inflater.inflate(
R.layout.pausebutton, null);
fl.addView(new MainGamePanel(this));
fl.addView(pause);
v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
}