所以我正在编写我的第一个Android小游戏,使用名为Panel的SurfaceView,它是一个单独的类而不是GameActivity的子类。当游戏结束时,我想根据玩家赢或输的方式修改选项菜单,然后打开它。 有关如何做到这一点的任何建议吗?
GameActivity:
public class GameActivity extends Activity {
private static final int MENU_PAUSE = 1;
private static final int MENU_RESUME = 2;
private static final int MENU_RESTART = 3;
private static final int MENU_QUIT = 4;
private Panel _panel;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
_panel = new Panel(this, 01);
setContentView(_panel);
}
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, MENU_PAUSE, 0, "Pause");
menu.add(0, MENU_RESUME, 0, "Resume");
menu.add(0, MENU_RESTART, 0, "Restart");
menu.add(0, MENU_QUIT, 0, "Quit");
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_PAUSE:
_panel.pause();
return true;
case MENU_RESUME:
_panel.resume();
return true;
case MENU_RESTART:
_panel = new Panel(this, 01);
setContentView(_panel);
case MENU_QUIT:
_panel.stop();
return true;
}
return false;
}
public void openMenu(){
openOptionsMenu();
}}
小组(短片):
class Panel extends SurfaceView implements SurfaceHolder.Callback, SensorEventListener {
// Game Vars
public Panel(Context context, int level) {
super(context);
_holder = getHolder();
_holder.addCallback(this);
_thread = new GameThread(getHolder(), this);
_handler = new Handler();
_context = context;
setFocusable(true);
start();
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
public void surfaceCreated(SurfaceHolder holder) {
_thread.setRunning(true);
_thread.start();
}
public void surfaceDestroyed(SurfaceHolder holder) {
// simply copied from sample application LunarLander:
// we have to tell thread to shut down & wait for it to finish, or else
// it might touch the Surface after we return and explode
boolean retry = true;
_thread.setRunning(false);
while (retry) {
try {
_thread.join();
retry = false;
} catch (InterruptedException e) {
// we will try it again and again...
}
}
}
private void win(){
}
private void lose(){
}
}