从Cocos2d场景推送ViewController

时间:2014-05-13 11:04:26

标签: ios iphone objective-c cocos2d-iphone

我正在故事板中加载一个cocos2d游戏。我实现了一个CocosViewController来加载启动游戏的cocos2d场景(IntroLayer)和IntroLayer。当游戏中的计时器(倒计时)为零时,我想回到CocosViewController或打开一个新的viewcontroller。

当倒计时到达零并且[[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];
    }

这就是游戏。我想在游戏结束时加载一个viewcontroller。

-(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 个答案:

答案 0 :(得分:0)

对于pop IntroLayer To ViewController如下代码

  [[[CCDirector sharedDirector] navigationController] popViewControllerAnimated:YES];