问题
Currently, I'm trying to build a game with UIkit that consists of the three MVCs (for now): main application delegate class (with UINavigationController property), Menu class (UIViewController with xib file) and game content class (also UIViewController with xib file). Obviously, the game needs methods for loading the game state, preparing to play, pausing the game and saving the game state. IMHO, these methods belong to game content MVC, so I put them there.
Now, everything is clear when the process is flat: app starts, navigation controller initializes, menu pops, you are making a click, game starts. But I'm trying to understand how to handle the situation when the game app enters the background or terminates. I see those nice methods such as "applicationDidBecomeActive", "applicationWillResignActive", "applicationWillTerminate" in the main app delegate class file, and I believe my game app architecture should be designed in a way that uses those methods for game prep stuff. What is your opinion, do I have to have a property (pointing to game content class) on main app delegate class in order to access game prep methods? Or do I have to redesign my game content mvc?
Update: My game content class is alloc'ed, init'ed and assigned to the main game app property (for pausing, saving state and etc in applicationWillResignActive, applicationDidBecomeActive...). The main app delegate class methods will handle all the preparation stuff. In addition, my menu class also points (have a property) to the same game content object, because I have a "new game", "resume game" buttons on the menu. Now, how should I restart the game from the main menu "new game" button? I would like to "delete" game content object and then alloc/init the new one. But the problem is, if I'm releasing the old and alloc/init'ing the new game content object from the menu class (and assigning to menu class property) then how can I pass the reference to that object to main app delegate class? Do I have to have a pointer to app main delegate object from my menu object property? I guess the game content should be kind of a singleton...
回答1:
You're quite right in wanting to use the UIApplicationDelegate signals, that's what they're for. For example:
-(void)applicationDidBecomeActive:(UIApplication*)app {
[my_game resume]; // ...or whatever
}
-(void)applicationWillResignActive:(UIApplication*)app {
[my_game suspend]; // ...or whatever
}
You shouldn't need to change anything in your game content to adapt to this (unless you did a really lousy job of it to begin with!). Make sure to initialise the game content in -application:didFinishLaunchWithOptions:
.
回答2:
Recently, I found a convenient way to grab a reference to main app delegate object for restarting (and starting) a gameplay - game content MVC. Here is the method code of my menu class that restarts/starts the game content:
-(IBAction) newGame{
GameContentViewController *gcvc = [[GameContentViewController alloc] init];
self.gameContentViewController = gcvc;
MyAppDelegate *myApp = [[UIApplication sharedApplication] delegate];
myApp.gameContentViewController = gcvc; //cia bus retain'intas referencas
[gcvc release];
[self.navigationController pushViewController:self.gameContentViewController animated:YES];
}
I'm retaining the game content obj only on main app delegate property. The property of menu that holds the reference to shared game object has only assign attribute. I thought it would be enough that the game content owner will be main app delegate only. I'm not sure about the cleanness of my app architecture (both main app delegate and menu class objects holds references to the same game obj via properties) but I gave up, cos I do not have another ideas :)
来源:https://stackoverflow.com/questions/6478160/how-should-correctly-look-the-simple-game-architecture