Reputation: 711
The coco2d game which is UIView is launched from viewcontroller as such
coco2dgame=[[coco2d_view alloc] initWithFrame:CGRectMake(0,0,320,480)];
[self.view addSubview:coco2dgame];
when it ends
[coco2dgame.director end];
[coco2dgame removeFromSuperview];
coco2dgame=nil;
when I want to relaunch I call again
coco2dgame=[[coco2d_view alloc] initWithFrame:CGRectMake(0,0,320,480)];
[self.view addSubview:coco2dgame];
but I am getting errors
OpenGL error 0x0502 in -[CCSprite draw] 532
OpenGL error 0x0502 in -[CCSprite draw] 532
Upvotes: 2
Views: 625
Reputation: 538
Your code setup seems to be a little whacky. You are loading cocos2d on a UIView, but retaining the director (a ViewController) on that view. Launching and ending the cocos2d engine within a ViewController structure can be a bit tricky, but this is what I use in my game:
Step 1: Modify the standard code in AppDelegate.m from the template. You need to comment out all of the lines dealing with the _director ivar, and change the root view controller from _director to your ViewController. The game will now launch into that ViewController within the navController created in the cocos2d template code instead of the _director.
Step 2: The ViewController that you're launching cocos2d from needs to have a method that is called in -init that creates and, this is important, retains the CCGLView that the director uses as its view, like so:
AppController *app = (AppController *)[[UIApplication sharedApplication] delegate];
myGLView = [[CCGLView viewWithFrame:[app.window bounds]
pixelFormat:kEAGLColorFormatRGB565 //kEAGLColorFormatRGBA8
depthFormat:0 //GL_DEPTH_COMPONENT24_OES
preserveBackbuffer:NO
sharegroup:nil
multiSampling:NO
numberOfSamples:0] retain];
Keeping the CCGLView around is important to preventing some OpenGL errors, as cocos2d seems to have an issue recreating it after it is destroyed. Like I said, this method is called only once, in the -init method of the ViewController you are launching cocos2d from.
Step 3: Create a method in your ViewController to setup the director and push it onto the navigation controller's stack, like so:
AppController *app = (AppController *)[[UIApplication sharedApplication] delegate];
CCDirectorIOS* director = (CCDirectorIOS *) [CCDirector sharedDirector];
director.wantsFullScreenLayout = YES;
[director setView:myGLView];
// Display FSP and SPF
[director setDisplayStats:NO];
// set FPS at 60
[director setAnimationInterval:1.0/60];
// for rotation and other messages
[director setDelegate:app];
// 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");
if (director.runningScene)
[director replaceScene:[TwoPlayerBoard node]];
else
[director pushScene:[TwoPlayerBoard node]];
[app.navController pushViewController:director animated:YES];
Most of that code is actually copied from the commented out code in AppDelegate where the original director would have been setup. Notice, you set the director's view to the CCGLView you create and retain in your ViewController's -init. In this method, you have the newly created director push your startup scene.
Step 4: Inside the game layer, whenever you want to return to your ViewController that the game (cocos2d) is launched from, implement this code:
AppController *app = (AppController *)[[UIApplication sharedApplication] delegate];
[app.navController popViewControllerAnimated:YES];
[[CCDirectorIOS sharedDirector] end];
That should allow you to move freely from a view controller, to the director in cocos2d (also a sub-class of UIViewController), and back without any issues. Hopefully that explains it in sufficient detail for you, let me know how it goes!
Upvotes: 2