0

我有一个正在使用surfaceview 开发的游戏。

它具有surfaceview 将采用的常规方法。

onDraw(){

Drawing here

 }

updateGame(){

Update logic here

}

run() {

while (isRunning){

onDraw()
updateGame()
}

}

所以目前我有一个启动画面、一个选项屏幕和游戏本身。每个都是一个单独的活动(因此有它自己的类)。

但是每个人都挤在一个班级(甚至是游戏)中,很难看到发生了什么。

在我的选项屏幕中,用户可以执行一些操作(例如单击帮助按钮或设置按钮),但我不确定如何启动另一个类但仍保持在相同的活动中。

即,我想使用我在当前班级中设置的画布并在不同的班级中绘制它。这可能吗?我找不到任何关于这样做的信息。

我了解 java 类的概念,我只是不确定如何将它们应用于我的情况。

还是做我正在做的事情并为每个“部分”创建新活动是一个更好的主意?

4

3 回答 3

2

使用片段:http: //developer.android.com/guide/components/fragments.html

它们将允许您将 Activity 变成一堆更小的、可嵌套的部分。片段代表屏幕的一部分,可以控制自己的行为,就像一个活动一样。它们是在 Honeycomb 中添加的,但 Google 为早期版本提供了支持包。据我所知,谷歌正试图让它们成为标准做法。

于 2013-02-26T14:53:26.617 回答
0

不使用类很难制作游戏。

您应该联系以保留更新活动中视图的代码。但是你应该将你在“updateGame()”中的代码完全抽象到另一个类中。我们将这个类称为业务类(Controller)。她负责组织比赛的进程

然后,如果您有要备份的信息,您应该使用另一个类来访问数据(数据访问层/模型)。

我建议你阅读这个: http: //www.leepoint.net/notes-java/GUI/structure/40mvc.html

如果你有很多动画和很多视频,你应该使用像http://www.andengine.org/这样的框架

带有新活动的示例菜单设置和游戏管理示例:

    public class MainActivity extends Activity {

    private static final int CODE_MENU_SETTING = 5;
    private int mTimeGame;
    private Thread mGameThread;
    private boolean isCurrentGame, isPause;
    private Button mStartGameButton;

    private ClassBusinessGame mBusiness;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //start button if you want to start the game with a button
        mStartGameButton = (Button) findViewById(/*id button start game*/);
        isCurrentGame = false;
        isPause = false;
        //mTimeGame counts the number of seconds of game
        mTimeGame = 0;
        //the class that controls the game
        mBusiness = new ClassBusinessGame(...); 
        //example thread timer
        mGameThread = new Thread("game thread") {
            public void run() {
                while(isCurrentGame) {
                    //here, your code to update your game, view
                    mTimeGame++;
                    try {
                        Thread.sleep(1000); //pause 1 sec
                    } catch(InterruptedException e) {}
                    while(isPause) {/*load*/} //this is not a good practice to break; loop while paused
                }
            }
        };
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        // here, directly call the Menu Setting Activity
        // attention: here, stop the thread which manage your game
        isPause = true;
        Intent menuIntent = new Intent(this, MenuSettingActivity.class);
        startActivityForResult(menuIntent, CODE_MENU_SETTING); //startActivityForResult to resume game
        return true;
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==CODE_MENU_SETTING) {
            //resume game
            isCurrentGame = false;
        }
    }
}

这是一个基本示例,可以进行许多改进,但这是(没有框架的游戏的想法)

于 2013-02-26T14:46:23.333 回答
0

嗨,伙计,我认为您需要一个游戏引擎,或者您可以开发一个示例,例如

class Screen {
    public void onShow();
    public void onHide();
    public void onUpdate();
    public void onDraw(Canvas canvas);
}

class Engine {
    Screen mScreen;

    public void setScreen(Screen s) {
        mScreen = s;
    }

    ...
}

顺便说一句,Libgdx 是一个不错的选择 http://code.google.com/p/libgdx/

于 2013-02-26T15:05:53.873 回答