cocos2d 2.0-rc2: end the director and restart

2020-06-04 05:44发布

问题:

I have a cocos2d powered game that uses UIKit menues, so I only use the framework for one viewcontroller, which is the game itself. Also, it only has one scene. Since cocos2d 2.0 the director itself is a UIViewController subclass, so I just push it in my MenuViewController when the user taps a start button:

-(void)startGameButtonPressed {

    CCDirectorIOS* director = (CCDirectorIOS *) [CCDirector sharedDirector];


    // Create an CCGLView with a RGB565 color buffer, and a depth buffer of 0-bits
    self.glView = [CCGLView viewWithFrame:CGRectMake(0, 0, 480, 320)
                              pixelFormat:kEAGLColorFormatRGB565    //kEAGLColorFormatRGBA8
                              depthFormat:0 //GL_DEPTH_COMPONENT24_OES
                       preserveBackbuffer:NO
                               sharegroup:nil
                            multiSampling:NO
                          numberOfSamples:0];

    // attach the openglView to the director
    [director setView:glView];
    [director runWithScene:[GameLayer scene]];
    [director setDelegate:(id <CCDirectorDelegate>) [[UIApplication sharedApplication] delegate]];
    [self.navigationController pushViewController:director animated:YES];
}

This works fine for the first time the method is called, when the user starts the first game. When the game is over, I call [[CCDirector sharedDirector] end].

Most of the director setup is done in the appDelegate (it's taken unchanged from the default Cocos2d template). I only put the CCGLView as a retained property into my MenuViewController, because otherwise the app crashes when [[CCDirector sharedDirector] end] is called and the CCGLView is not retained. I think that might be a cocos2d bug. In [[CCDirector sharedDirector] end] the framework calls [self setView:nil], but it still tries to access the view later on (probably on another thread).

The problem now is that on the second call of my method above (when the user wants to start another game from the menu), startGameButtonPressed, the director gets pushed but the screen remains black. The game is running and responding, I just don't see anything. Can someone please help me out with that?

回答1:

OK, I had the same problem and I was able to "fix it".

When you set the CCGLView and [director setView:], even if you pop the controller the scene still exists. the only thing that happens is that the scene gets stopped.

So in order to have the "restart" working, you have to check if there is already a running scene (even if it's stopped, and instead of runWithScene: you use replaceScene:.

Here is my code so you can see:

- (void)setupCocos2D {
    CCGLView *glView = [CCGLView viewWithFrame:CGRectMake(0, 0, 320, 480)
                               pixelFormat:kEAGLColorFormatRGB565   //kEAGLColorFormatRGBA8
                               depthFormat:0    //GL_DEPTH_COMPONENT24_OES
                        preserveBackbuffer:NO
                                sharegroup:nil
                             multiSampling:NO
                           numberOfSamples:0];

// HERE YOU CHECK TO SEE IF THERE IS A SCENE RUNNING IN THE DIRECTOR ALREADY    
if(![director_ runningScene]){
    [director_ setView:glView]; // SET THE DIRECTOR VIEW
    if( ! [director_ enableRetinaDisplay:YES] ) // ENABLE RETINA
        CCLOG(@"Retina Display Not supported");

    [director_ runWithScene:[HelloWorldLayer scene]]; // RUN THE SCENE

} else {
    // THERE IS A SCENE, START SINCE IT WAS STOPPED AND REPLACE TO RESTART
    [director_ startAnimation];
    [director_ replaceScene:[HelloWorldLayer scene]];
}      


[director_ setDelegate:(id <CCDirectorDelegate>) [[UIApplication sharedApplication] delegate]];

// I DO NOT PUSH BECAUSE I ALREADY PUSHED TO THIS CONTROLLER, SO I ADD THE COCOS2D VIEW AS A SUBVIEW
[self.view addSubview:[director_ view]];

}

Hope this code will help you, because I spent a whole day trying to figure this out. It may not be the correct way or even the prettiest way, but it's working :)

EDIT: Also, please not that if you POP the COCOS2D scene, you don't have to [[CCDirector sharedDirector] end] as the animation will be stopped when the view is dealloc/removed.



回答2:

I have spent several days looking for information relating to this, and will share with you my own experience. I am also trying to create a game that loads into a UITableViewController, from which the CCDirector is loaded when a cell is touched. This is a Game Center turn-based game, hence the design (think Words With Friends). The best approach I have found so far for this is as follows (note I'm working in 2.0 - where CCDirector is a UIViewController subclass):

In AppDelegate.h, create a new ivar to hold the CCGLView that is created from the template code. Then assign the CCGLView created in didFinishLaunching to your new ivar. This allows the director to reuse the originally created view instead of trying to recreate it every time you reload the CCDirector, which seems to cause all sorts of weird issues in my experience.

You also want to create a new method in AppDelegate called -setupDirector or something of the like where you will, well, setup the director. This should be called each time you are recreating the CCDirector. I have posted my version below. Note my ivar for the CCGLView is called "GLView".

- (void)setupDirector {
if (![CCDirector sharedDirector]) {
    CCLOG(@"Calling setupDirector");

    director_ = (CCDirectorIOS*) [CCDirector sharedDirector];

    director_.wantsFullScreenLayout = YES;

    // Display FSP and SPF
    [director_ setDisplayStats:NO];

    // set FPS at 60
    [director_ setAnimationInterval:1.0/60];

    // attach the openglView to the director
    [director_ setView:GLView];

    // for rotation and other messages
    [director_ setDelegate:self];

    // 2D projection
    [director_ setProjection:kCCDirectorProjection2D];
    //  [director setProjection:kCCDirectorProjection3D];

    // Enables High Res mode (Retina Display) on iPhone 4 and maintains low res on all other devices
    if( ! [director_ enableRetinaDisplay:YES] )
        CCLOG(@"Retina Display Not supported");

    // Default texture format for PNG/BMP/TIFF/JPEG/GIF images
    // It can be RGBA8888, RGBA4444, RGB5_A1, RGB565
    // You can change anytime.
    [CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA8888];

    // If the 1st suffix is not found and if fallback is enabled then fallback suffixes are going to searched. If none is found, it will try with the name without suffix.
    // On iPad HD  : "-ipadhd", "-ipad",  "-hd"
    // On iPad     : "-ipad", "-hd"
    // On iPhone HD: "-hd"
    CCFileUtils *sharedFileUtils = [CCFileUtils sharedFileUtils];
    [sharedFileUtils setEnableFallbackSuffixes:NO];             // Default: NO. No fallback suffixes are going to be used
    [sharedFileUtils setiPhoneRetinaDisplaySuffix:@"-hd"];      // Default on iPhone RetinaDisplay is "-hd"
    [sharedFileUtils setiPadSuffix:@"-ipad"];                   // Default on iPad is "ipad"
    [sharedFileUtils setiPadRetinaDisplaySuffix:@"-ipadhd"];    // Default on iPad RetinaDisplay is "-ipadhd"

    // Assume that PVR images have premultiplied alpha
    [CCTexture2D PVRImagesHavePremultipliedAlpha:YES];
}

In addition, you will want to make a couple changes to the way the template loads up the view controllers. Normally, cocos2D sets up the navigation controller with director_ as the root view controller. Here, you want to alloc and init your menu view controller and add THAT instead of director_:

// Create a Navigation Controller with the Director
gamesTVC_ = [[GamesTableViewController alloc] initWithStyle:UITableViewStyleGrouped];
navController_ = [[UINavigationController alloc] initWithRootViewController:gamesTVC_];
navController_.navigationBarHidden = NO;

Everything else in didFinishLaunching can remain the same. Now, in your menuViewController in your startGameButtonPressed method, you will call the newly created setupDirector method on your app instance, which is referenced by calling:

AppController *app = (AppController *)[[UIApplication sharedApplication] delegate];
if ([CCDirector sharedDirector].runningScene) {
    [[CCDirectorIOS sharedDirector] end];
}
[app setupDirector];
[app.navController pushViewController:app.director animated:YES];

I include a check to make sure the CCDirector isn't still running, and if it is, end it. In your game layer, when the time comes that you want to pop the view controller, you will simply call it like so:

AppController *app = (AppController *)[[UIApplication sharedApplication] delegate];
[app.navController popViewControllerAnimated:YES];
[[CCDirector sharedDirector] end];

This flow should allow you to freely use a navigation controller to push your game scene with CCDirector, and pop that view controller when you want to go back to your UIKit-based main menu. I hope this helps, as I have spent a lot of frustrating time trying to get this right for my own game.



回答3:

What works well is to just call startAnimation and stopAnimation in the director but keep the cocos2d view around and just re-use it.

Any attempts to shut down cocos2d and its OpenGL view and re-initializing it later on will cause more or less issues, because it really hasn't been tested well enough. Other than that cocos2d works just fine with UIKit, it just has the same issues any other OpenGL ES app has when mixing it with UIKit views.



回答4:

In my experience, Cocos doesn't really support ending and resuming, it acts like it's done and pretty much shuts itself down.

There are two things you can try, the first one (and it just came to my mind) is to call CC_DIRECTOR_INIT (may not be the exact name) after the director ended, and before you want to start it again.

The second one is to edit the director source code, and modify the end method so that it leaves cocos in a usable state (stop it from releasing the view and purging the cache, etc). Alternatively to this, you can modify the start method so that it makes sure Cocos is in a usable state before starting.

Sadly, Cocos doesn't make it easy for us to use UIKit+Cocos, but with luck with it.