从 Cocos2d 场景推送 ViewController
Posted
技术标签:
【中文标题】从 Cocos2d 场景推送 ViewController【英文标题】:Push a ViewController from a Cocos2d scene 【发布时间】:2014-05-13 11:04:26 【问题描述】:我在 Storyboard 中加载 cocos2d 游戏。我实现了一个加载 cocos2d 场景(IntroLayer)的 CocosViewController 和启动游戏的 IntroLayer。当游戏中的计时器(倒计时)为零时,我想回到 CocosViewController 或打开一个新的视图控制器。
我尝试在倒计时归零时调用[[CCDirector sharedDirector] popScene];
并使用CocosViewController *cocos = [[CocosViewController alloc] init]; [cocos.navigationController dismissViewControllerAnimated:YES completion:nil];
在 onExit 方法中,但当倒计时归零时应用程序崩溃,有什么建议吗?
我的 CocosViewController 设置 cocos2d
-(void)setupCocos2d
CCDirector *director = [CCDirector sharedDirector];
//[[[self navigationController] navigationBar] setHidden:YES];
if([director isViewLoaded] == NO)
// Create the OpenGL view that Cocos2D will render to.
CCGLView *glView = [CCGLView viewWithFrame:self.view.bounds
pixelFormat:kEAGLColorFormatRGB565
depthFormat:0
preserveBackbuffer:NO
sharegroup:nil
multiSampling:NO
numberOfSamples:0];
// Assign the view to the director.
director.view = glView;
// Initialize other director settings.
[director setAnimationInterval:1.0f/60.0f];
[director enableRetinaDisplay:YES];
// Set the view controller as the director's delegate, so we can respond to certain events.
director.delegate = self;
// Add the director as a child view controller of this view controller.
[self addChildViewController:director];
// Add the director's OpenGL view as a subview so we can see it.
[self.view addSubview:director.view];
[self.view sendSubviewToBack:director.view];
// Finish up our view controller containment responsibilities.
[director didMoveToParentViewController:self];
// Run whatever scene we'd like to run here.
NSArray *parameters = [[NSArray alloc] initWithObjects:@"3", @"sound", @"countdown", nil];
if(director.runningScene)
[director replaceScene:[IntroLayer scene];
else
[director pushScene:[IntroLayer scene];
- (void)viewDidLoad
[super viewDidLoad];
NSLog(@"cocos2d controller viewdidload");
[self setupCocos2d];
-(void)viewDidUnload
NSLog(@"did unload");
[[CCDirector sharedDirector] setDelegate:nil];
[[CCDirector sharedDirector] end];
IntroLayer 为游戏创建场景。这是我的 onExit 方法:
-(void) onExit
NSLog(@"Introscene exit");
[super onExit];
这就是游戏。我想在游戏结束时加载一个视图控制器。
-(void)update:(ccTime)dt
if (myTime > 0)
myTime -= (float)dt;
[timeLabel setString:[NSString stringWithFormat:@"%.2f", myTime]];
else
myTime = 0;
[timeLabel setString:[NSString stringWithFormat:@"0.00"]];
[[CCDirector sharedDirector] popScene];
if (clicks < currentClicks)
clicks = currentClicks;
//[clicksLabel setString:[NSString stringWithFormat:@"%i", clicks]];
-(void) onExit
[super onExit];
[[CCDirector sharedDirector] stopAnimation];
CocosViewController *cocos = [[CocosViewController alloc] init];
[cocos.navigationController dismissViewControllerAnimated:YES completion:nil];
// [cocos.navigationController popViewControllerAnimated:YES];
//AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
//[app.navController popViewControllerAnimated:YES];
//dismissModalViewControllerAnimated:YES
NSLog(@"game exit");
【问题讨论】:
【参考方案1】:For pop IntroLayer To ViewConrtroller 如下代码
[[[CCDirector sharedDirector] navigationController] popViewControllerAnimated:YES];
【讨论】:
以上是关于从 Cocos2d 场景推送 ViewController的主要内容,如果未能解决你的问题,请参考以下文章
如何从正在运行的(uiviewcontroller)应用程序启动 cocos2d 场景
如何以图形方式开发一些 ViewController,然后从 Cocos2d Iphone 2 场景中调用它们?