简体   繁体   中英

New view controller appears and then disappears behind CCScene

I'm working on integrating Ben Gottlieb's Twitter-OAuth-iPhone code into my cocos2d 0.99.5 project using this tutorial . I'm having some difficulties getting the view controller to properly load. I've never mixed cocos2d with standard Cocoa Touch UI things before and I'm a bit out of my depth.

I call the following code in my app delegate when it's time to connect to Twitter:

-(void) twitterAccountLogin
{
    UIViewController *controller = nil;
    if (!_engine) {
        _engine = [[SA_OAuthTwitterEngine alloc] initOAuthWithDelegate: self];
        _engine.consumerKey = kOAuthConsumerKey;
        _engine.consumerSecret = kOAuthConsumerSecret;

        controller = [SA_OAuthTwitterController controllerToEnterCredentialsWithTwitterEngine: _engine delegate: self];
    }

    if (controller) {
        [[CCDirector sharedDirector] stopAnimation];
        [viewController presentModalViewController:controller animated:YES];
        [controller release];
        return;
    }
} 

When this is called, the Twitter UIViewController is created, it animates onscreen, and then, as soon as it has finished animating (ie it reaches the top of the screen), it disappears. The currently running CCScene reappears, but it doesn't respond to touches. On the simulator, instead of the running scene reappearing, the screen turns black. In case it is unclear, viewController is the RootViewController recently added to cocos2d in 0.99.5.

It seems to me that the UIViewController is being created and then somehow is drawn under the running scene, but debugging has gotten me nowhere. Where have I gone wrong?

your view controller should be something like this:

[[[CCDirector sharedDirector]openGLView] addSubview: viewController.view]; 
[viewController presentModalViewController: controller animated:YES];

After searching for help here and on the cocos2d forums, here's what I have ended up doing.

-(void)pauseMenuButtonPressed
{    
    if(!paused)
    {
        paused = TRUE;

        [[CCDirector sharedDirector] pause];

        CGSize s = [[CCDirector sharedDirector] winSize];
        pauseLayer = [CCColorLayer layerWithColor: ccc4(150, 150, 150, 125) width: s.width height: s.height];
        pauseLayer.position = CGPointZero;
        [self addChild: pauseLayer z:8];

        CCMenuItem *pauseMenuItemResume =[CCMenuItemImage itemFromNormalImage:@"menuItemResumeSmall.png"
                                                            selectedImage: @"menuItemResumeSmallSelected.png"
                                                                   target:self
                                                                 selector:@selector(pauseMenuResumeSelected)];


        CCMenuItem *pauseMenuItemMainMenu =[CCMenuItemImage itemFromNormalImage:@"menuItemMainMenuSmall.png"
                                                              selectedImage: @"menuItemMainMenuSmallSelected.png"
                                                                     target:self
                                                                   selector:@selector(pauseMenuExitToMainMenuSelected)];

        // Create the pause menu and add the menu items to it
        pauseMenu = [CCMenu menuWithItems:pauseMenuItemResume, pauseMenuItemMainMenu, nil];

        // Arrange the menu items vertically
        [pauseMenu alignItemsVertically];

        // add the menu to the scene
        [self addChild:pauseMenu z:10];

        [hudButtons setIsTouchEnabled:NO];
    }

}

In short, rather than pushing a new scene for a pause menu, I pause all action in the game, create a transparent layer to overlay the screen, and then display the menu on top of that. This appears to be the conventional means of handling this problem in cocos2d, at least as of 0.99.5 (which is what the game is currently running in).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM