我知道已经有一些关于此的问题 - 但没有一个与我的问题有关。 我正在尝试通过 UINavigationController 将旧 Cocos2d 游戏的一部分附加到我的应用程序。 (这样做的要点是,当我想返回到我的应用程序的主屏幕时,我可以返回到我的 Root View Controller 。我认为对此有一个非常简单的解决方案..
*我想将游戏附加到作为我的 UINavigationController 元素的 View Controller 。如果我只是从我的 Root View Controller 运行游戏,下面的代码可以工作,但出于某种原因,如果我尝试从单独的 View Controller 运行游戏,它会给我一个“EXC_BAD_ACCESS Director @synchronized”错误。
目前,我正在使用以下代码从我的应用程序的主屏幕(根 VC)启动我的游戏(通过点击按钮)。
MyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
UIWindow *window = appDelegate.window;
[[UIApplication sharedApplication] setStatusBarStyle: UIStatusBarStyleBlackTranslucent];
Director *director = [Director sharedDirector];
[director setPixelFormat:kRGBA8];
[director attachInView:self.view withFrame: window.frame];
[director setAnimationInterval:1.0/kFPS];
[Texture2D setDefaultAlphaPixelFormat:kTexture2DPixelFormat_RGBA8888];
Scene *scene = [[Scene node] addChild:[Game node] z:0];
[director runWithScene: scene];
这可以很好地运行游戏。但是,我需要一些方法才能在完成游戏后返回到应用程序的主屏幕。如何在不是 Root View Controller 的 View Controller 中打开这个游戏?
[ROOT VC] --> [游戏 Controller :启动游戏]
我是 Cocos2d 的新手。非常感谢任何帮助!
最佳答案
我使用 Cocos2d Director 方法很容易地解决了这个问题:
[[Director sharedDirector] end]
这将结束所有正在运行的场景并将项目与其窗口或 View 分离。
因为我需要游戏全屏运行并使用导航 Controller ,所以我只使用下面的方法让它工作。
[appDelegate.navigationController setNavigationBarHidden:NO animated:YES];
[appDelegate.navigationController setNavigationBarHidden:YES animated:YES];
注意:您可以通过调用 [Director sharedDirector] 引用从项目中的任何位置调用任何 Director 方法。希望这可以帮助其他人并节省一些时间。
关于iphone - UINavigationController 中的 Cocos2d 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7549961/