实现 iPhone 游戏的主菜单 - 如何返回主菜单?
对于这么长的帖子表示歉意,我希望它对那里的人有一些意义。
我编写了一个iPhone游戏(在Quartz 2d中),它使用以下结构:
- 应用程序委托加载一个名为gameviewcontroller的视图控制器及其关联的视图
- 在“View did load”方法中,gameviewcontroller启动并启动一个游戏控制器类(NSObject) )。它还在游戏控制器中的“游戏循环”方法上启动计时器。最后,它告诉游戏控制器使用与游戏视图控制器相同的视图(这是一个自定义的 UIView)。
这一切都很好。我现在正在尝试集成游戏的主菜单。到目前为止,我已经完成了以下操作:
- 创建了一个名为“主菜单”的新视图控制器以及关联的 NIB。在 NIB 中,我创建了带有“开始”按钮的主菜单。
- 更改应用程序委托以加载 Main Manu NIB 并显示其视图。
- 设置一个方法,以便在按下按钮时加载游戏视图控制器(这将有效地启动游戏)。
到目前为止一切顺利 - 按“开始”按钮开始游戏。但是......
现在的问题是我找不到游戏控制器调用主菜单类的方法(例如,当游戏结束时)。我不能使用“selfmissModalViewController”,因为游戏控制器是 NSObject 类而不是视图控制器。如何让游戏控制器调出我的主菜单?
感谢大家的阅读,
马丁
Apologies for the long post and I hope it makes some sense to someone out there.
I have written an iPhone game (in Quartz 2d) which uses the following structure :
- App delegate loads a view controller called gameviewcontroller and its associated view
- In the "View did load" method, the gameviewcontroller starts up and initiates a Game Controller class (NSObject). It also starts a timer on a "Game Loop" method in the Game Controller. Finally it tells the Game Controller to use the same view as the gameview controller (which is a custom UIView).
This all works fine. I am now trying to integrate a Main Menu for the game. So far I have done the following :
- Created a new View Controller called "Main Menu" with an associated NIB. In the NIB I have created my main menu with a "Start" button.
- Altered the app delegate to load the Main Manu NIB and display its view.
- Set a method so that when the button is pressed it then loads the gameviewcontroller (which effectively starts the game).
So far so good - pressing the "start" button starts the game. But.....
The problem is now that I can't find a way for the Game Controller to call up the Main Menu class (e.g for when game is over). I can't use "self dismissModalViewController" as Game Controller is an NSObject class and not a view controller. How can I get the Game Controller to pull up my Main Menu ?
Thanks all for reading,
Martin
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的菜单对象仍然存在,只需调用它的“dissmisModalViewController”即可。
例如 [[MainMenu getInstance]dismissModalViewControllerAnimated:YES];其中 getInstance 返回您的对象或将其作为属性存储在 GameController 中,因此当您从 MainMenu 或 GameViewController 创建 GameController 时,只需将其自身指定为其属性 gameInstance.mainMenu = self;
如何制作 getInstance 方法:
您可以使用单例模式(您可以从苹果开发网站获取一个),或者如果您手动创建 MainMenu,您可以只在某个全局变量中记住 self ,而 getInstance 将是类方法,如下所示
:实施中
希望这会有所帮助,
Krzysztof Zabłocki
If you have your menu object still living just call its "dissmisModalViewController".
for example [[MainMenu getInstance] dismissModalViewControllerAnimated:YES]; where getInstance returns your object or have it stored in GameController as property, so when you create GameController from your MainMenu or GameViewController just assign itself as his property gameInstance.mainMenu = self;
How-to make getInstance method:
You could either use Singleton pattern ( you can get one from apple dev site ) or if you manually create MainMenu you could just remember self in some global variable and getInstance would be class method, something like that:
and in implementation
Hope this helps,
Krzysztof Zabłocki