不使用类很难制作游戏。
您应该联系以保留更新活动中视图的代码。但是你应该将你在“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;
}
}
}
这是一个基本示例,可以进行许多改进,但这是(没有框架的游戏的想法)