【问题标题】:Using a new class? Android Game-dev使用新课程? Android 游戏开发
【发布时间】:2013-02-26 14:28:28
【问题描述】:

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

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

onDraw(){

Drawing here

 }

updateGame(){

Update logic here

}

run() {

while (isRunning){

onDraw()
updateGame()
}

}

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

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

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

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

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

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

【问题讨论】:

    标签: android class object android-activity structure


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      不使用类很难制作游戏。

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

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

      我建议您阅读以下内容: http://www.leepoint.net/notes-java/GUI/structure/40mvc.html

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

      新 Activity 的菜单设置示例和游戏管理示例:

          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;
              }
          }
      }
      

      这是一个基本的例子,有很多改进是可能的,但这是一个想法(没有框架的游戏)

      【讨论】:

      • 谢谢@juliendumortier,所以,如果在我的情况下,我有一个选项屏幕,上面有一个“设置”按钮。当用户按下该按钮时,我应该开始另一个活动吗? (这就是我目前的做法) - 或者我应该留在同一个班级和活动中。我理解你所说的拆分逻辑和绘图,但是我上面描述的不同“部分”怎么样?我正在开始一项新活动,因为这样我就可以创建一个新班级并保持一切整洁,但我不确定是否有办法在保持相同活动的同时做到这一点?
      • 就我而言,我的大多数应用程序要显示设置菜单,我处于相同的活动中,但我使用了 AlertDialog。这避免了切换到另一个活动。但是您可以很好地将新活动仅用于您的菜单设置。这里没有地方,我将用示例代码编辑答案。
      【解决方案3】:

      你好,我认为你需要一个游戏引擎,或者你可以开发一个类似的示例

      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/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-07-12
        • 1970-01-01
        • 2014-12-19
        • 2014-04-13
        • 1970-01-01
        • 1970-01-01
        • 2014-06-09
        相关资源
        最近更新 更多