Вопрос

I am loading a cocos2d game inside an Storyboard. I implemented a CocosViewController that loads the cocos2d scene (IntroLayer) and IntroLayer that starts the game. I would like to go back to CocosViewController or open a new viewcontroller when the timer (countdown) is zero in the game.

I tried calling [[CCDirector sharedDirector] popScene]; when the countdown arrives to zero and with CocosViewController *cocos = [[CocosViewController alloc] init]; [cocos.navigationController dismissViewControllerAnimated:YES completion:nil]; in onExit method but the app crashes when the countdown arrives to zero, any suggestion?

My CocosViewController set up 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 creates the scene for the game. This is my onExit method:

 -(void) onExit{
        NSLog(@"Introscene exit");
        [super onExit];
    }

And this is the game. I want to load a viewcontroller when the game finished.

-(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");
}
Это было полезно?

Решение

For pop IntroLayer To ViewConrtroller As below code

  [[[CCDirector sharedDirector] navigationController] popViewControllerAnimated:YES];
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top