应该如何正确看待简单的游戏架构? [英] How should correctly look the simple game architecture?

查看:23
本文介绍了应该如何正确看待简单的游戏架构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在尝试使用由三个 MVC 组成的 UIkit 构建游戏(目前):主应用程序委托类(带有 UINavigationController 属性)、菜单类(带有 xib 文件的 UIViewController)和游戏内容类(还有带有 xib 文件的 UIViewController).显然,游戏需要加载游戏状态、准备游戏、暂停游戏和保存游戏状态的方法.恕我直言,这些方法属于游戏内容MVC,所以我把它们放在那里.

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.

现在,当过程平坦时,一切都清楚了:应用程序启动,导航控制器初始化,菜单弹出,您点击一下,游戏开始.但我试图了解如何处理游戏应用程序进入后台或终止时的情况.我在主应用程序委托类文件中看到了诸如applicationDidBecomeActive"、applicationWillResignActive"、applicationWillTerminate"之类的好方法,我相信我的游戏应用程序架构的设计方式应该将这些方法用于游戏准备工作.您的意见是,我是否必须在主应用程序委托类上拥有一个属性(指向游戏内容类)才能访问游戏准备方法?还是我必须重新设计我的游戏内容 mvc?

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?

更新:我的游戏内容类已分配、初始化并分配给主游戏应用程序属性(用于在 applicationWillResignActive、applicationDidBecomeActive...中暂停、保存状态等).主要的应用程序委托类方法将处理所有准备工作.另外,我的菜单类也指向(有一个属性)同一个游戏内容对象,因为我的菜单上有新游戏"、继续游戏"按钮.现在,我应该如何从主菜单的新游戏"按钮重新启动游戏?我想删除"游戏内容对象,然后分配/初始化新的.但问题是,如果我从菜单类中释放旧的和分配/初始化新的游戏内容对象(并分配给菜单类属性),那么我如何将该对象的引用传递给主应用程序委托类?我是否必须从我的菜单对象属性中获得指向应用程序主委托对象的指针?我想游戏内容应该是一种单身......

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...

推荐答案

您想使用 UIApplicationDelegate 信号是完全正确的,这就是它们的用途.例如:

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
}

您不需要更改游戏内容中的任何内容来适应这种情况(除非您一开始就做得非常糟糕!).确保在-application:didFinishLaunchWithOptions:中初始化游戏内容.

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:.

这篇关于应该如何正确看待简单的游戏架构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆