3

所以我为 Android 编写了我的第一个小游戏,使用了一个名为 Panel 的 SurfaceView,它是一个单独的类,而不是 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(){

    }


}
4

0 回答 0