Ios 从CCLayer开始制作故事板片段
我有一个项目,我使用故事板,一个导航控制器,和故事板序列。我在其中一个视图中有一个cocos2d视图。视图Ios 从CCLayer开始制作故事板片段,ios,objective-c,cocos2d-iphone,storyboard,Ios,Objective C,Cocos2d Iphone,Storyboard,我有一个项目,我使用故事板,一个导航控制器,和故事板序列。我在其中一个视图中有一个cocos2d视图。视图GameLayer具有类CCLayer,该类嵌入在cocos2dViewControllercocos2dViewController:CCViewController中,而CCViewController又是CCViewController:UIViewController的子类。因此,在我的Cocos2d视图中,它是一个CCLayer,不能使用[self-performsgue…]方法,
GameLayer
具有类CCLayer
,该类嵌入在cocos2dViewControllercocos2dViewController:CCViewController
中,而CCViewController又是CCViewController:UIViewController
的子类。因此,在我的Cocos2d视图中,它是一个CCLayer,不能使用[self-performsgue…]方法,因为它不是uiviewcontroller或其他东西的直接子类
我试过了
GameOverViewController *gameOver = [[GameOverViewController alloc]init];
UIViewController* rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
[rootViewController presentViewController:gameOver animated:YES completion:NULL];
但这让我得到了警告:尝试显示不在窗口层次结构中的视图代码>
我也试过了
GameOverViewController *gameOver = [[GameOverViewController alloc]init];
AppController *app = (AppController*) [[UIApplication sharedApplication] delegate];
[[app navController] presentModalViewController:gameOver animated:YES];
但那根本没用
因此,我需要在其中一个更高的类上执行选择器,这是UIViewController的子类,在这种情况下,我该如何做呢
或者我需要从我的CCLayer执行segue,在这种情况下,我该如何做?这两种方法都可以:)
编辑:澄清问题:
我需要从我的GameLayer做一个segue,它是一个CCLayer
子类,因此它不能使用正常的segue方法。GameLayer嵌入在CoCoCos2dViewController
中,它是CCViewController
的子类,最后是UIViewController的子类。我在项目中使用故事板,CCViewController是响应IBOutlets的视图控制器。所以我需要从我的游戏层推送这个文件中的segue。你可以在这里看到游戏系统:从我制作的模板。我想把一段从游戏视图控制器拉到另一个视图。到目前为止,我在另一个文件ccdirector和其他文件中尝试了各种方法
对于那些不熟悉cocos2d的人来说,CCLayer是CCNode的一个子类,CCNode是NSObject的一个子类。因此,您可以将其视为一个关于如何从NSObject在UIViewController中执行segue的问题
So tl;dr:从嵌入UIViewController子类的子类中的CCLayer(NSObject)执行故事板分段 我不建议在使用
Cocos2d
时使用segues
。相反,您应该将游戏作为全新的CCScene
或CCLayer
显示在屏幕上,只需将其作为父节点的子节点添加即可。如果您的gameover屏幕是某种弹出/对话框,您也可以尝试使用透明的黑色背景色
GameOverViewController *gameOver = [self.storyboard instantiateViewControllerWithIdentifier:@"GameOverViewController"];
UINavigationController *navController=[[UINavigationController new]initWithRootViewController:gameOver];
[self.navigationController presentViewController:navController animated:YES completion:Nil];
您应该尝试下面显示相同错误的链接--警告:尝试显示:0xab5d9d0,其视图不在窗口层次结构中!
我完全同意ssantos及其解决方案,但如果您坚持尝试将UIKit和cocos2D混搭在一起: 由于控制器是UIViewController的子类,请尝试使用
[[CCDirector sharedDirector]performsgue….]
,而不是“[self-performsgue….]
”
我不能肯定这是否有效,因为您正在以一种非传统的方式使用cocos2D
另一项意见:
您说您的
GameLayer
嵌入了CoCoCos2dViewController
。。它是否有对该视图控制器的引用?你能打电话给performSegue吗?好的,我想我已经成功了。我尝试了很多方法,主要的问题是游戏层是CCLayer,很难找到包含它的ViewController
您需要一个对cocos2dViewController ViewController的引用来执行segue,我的解决方案并不理想,但在这种情况下它可以工作
我使用NSNotificationCenter发布来自游戏玩家的通知,然后添加cocos2dViewController作为观察者。当cocos2dViewController收到通知时,您将执行该步骤
[self performSegueWithIdentifier:@"segueFromCocos" sender:self];
具体步骤如下:
UIButton *nextButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[nextButton setTitle:@"next" forState:UIControlStateNormal];
nextButton.tag = 123;
nextButton.frame = CGRectMake(10.0, 10.0, 90.0, 40.0);
[nextButton addTarget:self
action:@selector(nextButtonClicked:)
forControlEvents:UIControlEventTouchDown];
UIView *glView = [CCDirector sharedDirector].view;
[glView addSubview:nextButton];
此按钮将按下下一个按钮点击:选择器中的通知
- (void)nextButtonClicked:(id)发送方 { NSLog(“已发布通知”); [[NSNotificationCenter defaultCenter]postNotificationName:@“TestNotification”对象:self]; }
- (void)asdf
{
[self performSegueWithIdentifier:@"segueFromCocos" sender:self];
}
- (void)asdf
{
[self performSegueWithIdentifier:@"segueFromCocos" sender:self];
}
- (无效)接收测试通知:(NSNotification*)通知 { if([[notification name]IsequalString:@“TestNotification”]){ NSLog(@“已成功接收测试通知!%@”,[self class]) } }
[navC.viewControllers lastObject];
[navC.viewControllers lastObject];
这是触摸“下一步”按钮时运行的代码:
UIViewController *root = [[[UIApplication sharedApplication] delegate] window].rootViewController;
if ( [root isKindOfClass:[UINavigationController class]] ) {
UINavigationController *navC = (UINavigationController *)root;
cocos2dViewController *cocosVC = (cocos2dViewController *)[navC.viewControllers lastObject];
[cocosVC performSelector:@selector(asdf)];
}
UIViewController *root = [[[UIApplication sharedApplication] delegate] window].rootViewController;
if ( [root isKindOfClass:[UINavigationController class]] ) {
UINavigationController *navC = (UINavigationController *)root;
cocos2dViewController *cocosVC = (cocos2dViewController *)[navC.viewControllers lastObject];
[cocosVC performSelector:@selector(asdf)];
}
然后在cocos2dViewController中有一个asdf选择器,它只执行
- (void)asdf
{
[self performSegueWithIdentifier:@"segueFromCocos" sender:self];
}